Grazie. A beneficio di tutti riporto di seguito il codice che ho tratto dai vari suggerimenti e che risolve il mio obiettivo (tabelle con numero variabile di colonne e testata opzionale).
`
\documentclass{article}
\usepackage[utf8]{inputenc}
\usepackage[italian]{babel}
\usepackage{calc}
\usepackage{tabularx}
\usepackage{booktabs}
\usepackage{xparse}
\usepackage{xcolor,colortbl}
\usepackage{soulutf8,color}
\usepackage{lipsum}
\usepackage{longtable}
\newcommand\McsTcw[1]{#1\textwidth-2\tabcolsep}
\newcommand\McsTh[2][McsColTestTbl]{\cellcolor{#1}\textbf{\textit{#2}}}
\definecolor{McsColTestTbl}{rgb}{.8,.8,.8}
\ExplSyntaxOn
\keys_define:nn { McsTabDefs } {%
testata .tl_set:N = \l_McsTab_Testata_tl,
col1 .tl_set:N = \l_McsTab_HColA_tl,
col2 .tl_set:N = \l_McsTab_HColB_tl,
col3 .tl_set:N = \l_McsTab_HColC_tl,
col4 .tl_set:N = \l_McsTab_HColD_tl,
col5 .tl_set:N = \l_McsTab_HColE_tl,
dim1 .tl_set:N = \l_McsTab_DimColA_tl,
dim2 .tl_set:N = \l_McsTab_DimColB_tl,
dim3 .tl_set:N = \l_McsTab_DimColC_tl,
dim4 .tl_set:N = \l_McsTab_DimColD_tl,
dim5 .tl_set:N = \l_McsTab_DimColE_tl,
}
\tl_new:N \l_McsTab_TabContens_tl
\tl_new:N \l_McsTab_TabColDefs_tl
\int_new:N \l_McsTab_NumCols_tl
\NewDocumentEnvironment{ McsTabGen } { mm } {%
\int_set:Nn \l_McsTab_NumCols_tl { #1 }
\keys_set:nn { McsTabDefs } { #2 }
\begin{center}
\small
\tl_put_right:No \l_McsTab_TabContens_tl {%
\begin{tabular} {
p{ \l_McsTab_DimColA_tl }
p{ \l_McsTab_DimColB_tl }
p{ \l_McsTab_DimColC_tl }
p{ \l_McsTab_DimColD_tl }
p{ \l_McsTab_DimColE_tl }
}
}
\tl_put_right:Nn \l_McsTab_TabContens_tl {%
\toprule
\tl_if_empty:NTF \l_McsTab_Testata_tl {} {
\multicolumn{\l_McsTab_NumCols_tl}{c}{\McsTh{\l_McsTab_Testata_tl}} \\
\midrule }
\McsTh{\l_McsTab_HColA_tl} &
\McsTh{\l_McsTab_HColB_tl}
}
\int_compare:nTF { \l_McsTab_NumCols_tl > 2 } {%
\tl_put_right:Nn \l_McsTab_TabContens_tl { & \McsTh{\l_McsTab_HColC_tl} }}{}
\int_compare:nTF { \l_McsTab_NumCols_tl > 3 } {%
\tl_put_right:Nn \l_McsTab_TabContens_tl { & \McsTh{\l_McsTab_HColD_tl} }}{}
\int_compare:nTF { \l_McsTab_NumCols_tl > 4 } {%
\tl_put_right:Nn \l_McsTab_TabContens_tl { & \McsTh{\l_McsTab_HColE_tl} }}{}
\tl_put_right:Nn \l_McsTab_TabContens_tl { \\ }
}{
\tl_use:N \l_McsTab_TabContens_tl
\bottomrule
\end{tabular}
\end{center}
}
\NewDocumentEnvironment{ McsTabFive } { m } {\begin{McsTabGen} {5} {#1} }{ \end{McsTabGen} }
\NewDocumentEnvironment{ McsTabFour } { m } {\begin{McsTabGen} {4} {#1} }{ \end{McsTabGen} }
\NewDocumentEnvironment{ McsTabThree } { m } {\begin{McsTabGen} {3} {#1} }{ \end{McsTabGen} }
\NewDocumentEnvironment{ McsTabTwo } { m } { \begin{McsTabGen} {2} {#1} }{ \end{McsTabGen} }
\NewDocumentCommand{\McsRigaTabFive} { mmmmm } {%
\tl_put_right:Nn \l_McsTab_TabContens_tl { \midrule #1 & #2 & #3 & #4 & #5 \\ }
}
\NewDocumentCommand{\McsRigaTabFour} { mmmm } {%
\tl_put_right:Nn \l_McsTab_TabContens_tl { \midrule #1 & #2 & #3 & #4 \\ }
}
\NewDocumentCommand{\McsRigaTabThree} { mmm } {%
\tl_put_right:Nn \l_McsTab_TabContens_tl { \midrule #1 & #2 & #3 \\ }
}
\NewDocumentCommand{\McsRigaTabTwo} { mm } {%
\tl_put_right:Nn \l_McsTab_TabContens_tl { \midrule #1 & #2 \\ }
}
\NewDocumentEnvironment{McsLongTabGen} { mm } {%
\int_set:Nn \l_McsTab_NumCols_tl { #1 }
\keys_set:nn { McsTabDefs } { #2 }
\vspace*{-\baselineskip}
\begin{center}
\small
\tl_put_right:No \l_McsTab_TabContens_tl {%
\begin{longtable} {
p{ \l_McsTab_DimColA_tl }
p{ \l_McsTab_DimColB_tl }
p{ \l_McsTab_DimColC_tl }
p{ \l_McsTab_DimColD_tl }
p{ \l_McsTab_DimColE_tl }
}
}
% intestazione iniziale
\tl_put_right:Nn \l_McsTab_TabContens_tl {%
\toprule
\tl_if_empty:NTF \l_McsTab_Testata_tl {} {
\multicolumn{\l_McsTab_NumCols_tl}{c}{\McsTh{\l_McsTab_Testata_tl}} \\
\midrule }
\McsTh{\l_McsTab_HColA_tl} &
\McsTh{\l_McsTab_HColB_tl}
}
\int_compare:nTF { \l_McsTab_NumCols_tl > 2 } {%
\tl_put_right:Nn \l_McsTab_TabContens_tl { & \McsTh{\l_McsTab_HColC_tl} }}{}
\int_compare:nTF { \l_McsTab_NumCols_tl > 3 } {%
\tl_put_right:Nn \l_McsTab_TabContens_tl { & \McsTh{\l_McsTab_HColD_tl} }}{}
\int_compare:nTF { \l_McsTab_NumCols_tl > 4 } {%
\tl_put_right:Nn \l_McsTab_TabContens_tl { & \McsTh{\l_McsTab_HColE_tl} }}{}
\tl_put_right:Nn \l_McsTab_TabContens_tl { \\ \endfirsthead }
% intestazione inizio nuova pagina
\tl_put_right:Nn \l_McsTab_TabContens_tl {%
\multicolumn{\l_McsTab_NumCols_tl}{l}
{\begin{footnotesize}\textit{continua~dalla~pagina~precedente}\end{footnotesize}} \\
\toprule
\tl_if_empty:NTF \l_McsTab_Testata_tl {} {
\multicolumn{\l_McsTab_NumCols_tl}{c}{\McsTh{\l_McsTab_Testata_tl}} \\
\midrule }
\McsTh{\l_McsTab_HColA_tl} &
\McsTh{\l_McsTab_HColB_tl}
}
\int_compare:nTF { \l_McsTab_NumCols_tl > 2 } {%
\tl_put_right:Nn \l_McsTab_TabContens_tl { & \McsTh{\l_McsTab_HColC_tl} }}{}
\int_compare:nTF { \l_McsTab_NumCols_tl > 3 } {%
\tl_put_right:Nn \l_McsTab_TabContens_tl { & \McsTh{\l_McsTab_HColD_tl} }}{}
\int_compare:nTF { \l_McsTab_NumCols_tl > 4 } {%
\tl_put_right:Nn \l_McsTab_TabContens_tl { & \McsTh{\l_McsTab_HColE_tl} }}{}
\tl_put_right:Nn \l_McsTab_TabContens_tl { \\ \midrule \endhead }
% piede verso nuova pagina
\tl_put_right:Nn \l_McsTab_TabContens_tl {%
\multicolumn{\l_McsTab_NumCols_tl}{r}
{\begin{footnotesize}\textit{continua~nella~prossima~pagina}\end{footnotesize}} \\
\endfoot
}
% piede finale
\tl_put_right:Nn \l_McsTab_TabContens_tl {%
\bottomrule
\multicolumn{\l_McsTab_NumCols_tl}{r}
{\begin{footnotesize}\textit{si~conclude~dalla~pagina~precedente}\end{footnotesize}} \\
\endlastfoot
}
}{%
\tl_use:N \l_McsTab_TabContens_tl
\end{longtable}
\end{center}
}
\NewDocumentEnvironment{ McsLongTabFive } { m } { \begin{McsLongTabGen} {5} {#1} }{ \end{McsLongTabGen} }
\NewDocumentEnvironment{ McsLongTabFour } { m } { \begin{McsLongTabGen} {4} {#1} }{ \end{McsLongTabGen} }
\NewDocumentEnvironment{ McsLongTabThree } { m } { \begin{McsLongTabGen} {3} {#1} }{ \end{McsLongTabGen} }
\NewDocumentEnvironment{ McsLongTabTwo } { m } { \begin{McsLongTabGen} {2} {#1} }{ \end{McsLongTabGen} }
\NewDocumentCommand{\McsRigaLongTabFive} { mmmmm } {%
\tl_put_right:Nn \l_McsTab_TabContens_tl { \midrule #1 & #2 & #3 & #4 & #5 \\ }
}
\NewDocumentCommand{\McsRigaLongTabFour} { mmmm } {%
\tl_put_right:Nn \l_McsTab_TabContens_tl { \midrule #1 & #2 & #3 & #4 \\ }
}
\NewDocumentCommand{\McsRigaLongTabThree} { mmm } {%
\tl_put_right:Nn \l_McsTab_TabContens_tl { \midrule #1 & #2 & #3 \\ }
}
\NewDocumentCommand{\McsRigaLongTabTwo} { mm } {%
\tl_put_right:Nn \l_McsTab_TabContens_tl { \midrule #1 & #2 \\ }
}
\ExplSyntaxOff
\begin{document}
\lipsum[1]
\begin{McsTabFive}{
testata=Profili autorizzativi,
col1=Account, dim1=\McsTcw{.15},
col2=Certificatore, dim2=\McsTcw{.25},
col3=Dt. rilascio, dim3=\McsTcw{.2},
col4=Validità, dim4=\McsTcw{.2},
col5=Note, dim5=\McsTcw{.2}
}
\McsRigaTabFive{uuuu}{aaaaa aaaa aaaaaa}{xxx}{XX anni}{xxx}
\end{McsTabFive}
\lipsum[2]
\begin{McsTabFour}{
col1=Alfa, dim1=\McsTcw{.2},
col2=Beta, dim2=\McsTcw{.15},
col3=Gamma, dim3=\McsTcw{.2},
col4=Delta, dim4=\McsTcw{.3}
}
\McsRigaTabFour{aaa}{bbb}{ccc}{ddd}
\McsRigaTabFour{fff}{ggg}{hhh}{iii}
\end{McsTabFour}
\lipsum[3]
\begin{McsTabThree}{
testata=Nuova testata,
col1=Teta, dim1=\McsTcw{.33},
col2=Micro, dim2=\McsTcw{.33},
col3=Macro, dim3=\McsTcw{.33}
}
\McsRigaTabThree{kkk}{lll}{mmm}
\McsRigaTabThree{ppp}{qqq}{rrr}
\end{McsTabThree}
\lipsum[4]
\begin{McsLongTabFive}{
testata=Tabella Lunga con cinque colonne,
col1=Account, dim1=\McsTcw{.2},
col2=Certificatore, dim2=\McsTcw{.2},
col3=Dt. rilascio, dim3=\McsTcw{.2},
col4=Validità, dim4=\McsTcw{.2},
col5=Note, dim5=\McsTcw{.2}
}
\McsRigaLongTabFive
{There are three ways to define new functions. All classes define a function to expand to the substitution text. Within the substitution text the actual parameters are substituted for the formal parameters}
{Sets function to expand to code as replacement text. Within the code , the parameters}
{Configurazione degli apparati informatici per l'esecuzione di controlli automatizzati}
{Sessioni di collaudo e certificazione delle soluzioni di ripristino}
{h sdhg jg ashdg hjshghds ghsdh ghds ghasdhg}
\McsRigaLongTabFive
{There are three ways to define new functions. All classes define a function to expand to the substitution text. Within the substitution text the actual parameters are substituted for the formal parameters}
{Sets function to expand to code as replacement text. Within the code , the parameters}
{Configurazione degli apparati informatici per l'esecuzione di controlli automatizzati}
{Sessioni di collaudo e certificazione delle soluzioni di ripristino}
{h sdhg jg ashdg hjshghds ghsdh ghds ghasdhg}
\McsRigaLongTabFive
{There are three ways to define new functions. All classes define a function to expand to the substitution text. Within the substitution text the actual parameters are substituted for the formal parameters}
{Sets function to expand to code as replacement text. Within the code , the parameters}
{Configurazione degli apparati informatici per l'esecuzione di controlli automatizzati}
{Sessioni di collaudo e certificazione delle soluzioni di ripristino}
{h sdhg jg ashdg hjshghds ghsdh ghds ghasdhg}
\McsRigaLongTabFive
{There are three ways to define new functions. All classes define a function to expand to the substitution text. Within the substitution text the actual parameters are substituted for the formal parameters}
{Sets function to expand to code as replacement text. Within the code , the parameters}
{Configurazione degli apparati informatici per l'esecuzione di controlli automatizzati}
{Sessioni di collaudo e certificazione delle soluzioni di ripristino}
{h sdhg jg ashdg hjshghds ghsdh ghds ghasdhg}
\McsRigaLongTabFive
{There are three ways to define new functions. All classes define a function to expand to the substitution text. Within the substitution text the actual parameters are substituted for the formal parameters}
{Sets function to expand to code as replacement text. Within the code , the parameters}
{Configurazione degli apparati informatici per l'esecuzione di controlli automatizzati}
{Sessioni di collaudo e certificazione delle soluzioni di ripristino}
{h sdhg jg ashdg hjshghds ghsdh ghds ghasdhg}
\McsRigaLongTabFive
{There are three ways to define new functions. All classes define a function to expand to the substitution text. Within the substitution text the actual parameters are substituted for the formal parameters}
{Sets function to expand to code as replacement text. Within the code , the parameters}
{Configurazione degli apparati informatici per l'esecuzione di controlli automatizzati}
{Sessioni di collaudo e certificazione delle soluzioni di ripristino}
{h sdhg jg ashdg hjshghds ghsdh ghds ghasdhg}
\McsRigaLongTabFive
{There are three ways to define new functions. All classes define a function to expand to the substitution text. Within the substitution text the actual parameters are substituted for the formal parameters}
{Sets function to expand to code as replacement text. Within the code , the parameters}
{Configurazione degli apparati informatici per l'esecuzione di controlli automatizzati}
{Sessioni di collaudo e certificazione delle soluzioni di ripristino}
{h sdhg jg ashdg hjshghds ghsdh ghds ghasdhg}
\McsRigaLongTabFive
{There are three ways to define new functions. All classes define a function to expand to the substitution text. Within the substitution text the actual parameters are substituted for the formal parameters}
{Sets function to expand to code as replacement text. Within the code , the parameters}
{Configurazione degli apparati informatici per l'esecuzione di controlli automatizzati}
{Sessioni di collaudo e certificazione delle soluzioni di ripristino}
{h sdhg jg ashdg hjshghds ghsdh ghds ghasdhg}
\end{McsLongTabFive}
\lipsum[1]
\end{document}
`