次のように、カンマ区切りのデータに基づいてテーブルを作成したいと思います。
\documentclass{article}
\usepackage{pgffor}
\usepackage{multirow}
\usepackage{float}
\newcommand{\cases}{a , b}
\newcommand{\repetir}{
\foreach \case in {\cases}{
\case & & & & & & & & & & & & & & & \\\hline
}
}
\begin{document}
\begin{table}[H]
\centering
\scriptsize
\begin{tabular}{ | p{4cm} | p{3cm} |l| l|l|l| l | l | l | l | l| l | l | l | l | p{3cm} | }
\hline
\multirow{3}{*}{Case name} & \multirow{3}{*}{Description} & \multicolumn{14}{c|}{Further Details} \\\cline{3-16}
& & \multicolumn{3}{c|}{O} & \multicolumn{3}{c|}{A} & \multicolumn{2}{c|}{F} & \multicolumn{2}{c|}{B} & & \multirow{2}{*}{G} & \multirow{2}{*}{C} & \multirow{2}{*}{Other} \\ \cline{3-13}
& 1 & 2 & 3 & 4 & 5 & 6 & 7 & 8 & 9 & 10 & 11 & 12 & 13 & 14 & 15 \\ \hline
\repetir
\end{tabular}
\end{table}
\end{document}
しかし、常にエラーが発生します:
Extra } or forgotten \endgroup \repetir.
これどうやってするの?
答え1
\foreach
表のセルで開始して別のセルで終了することはできません。
\documentclass{article}
\usepackage[a4paper,landscape]{geometry}
\usepackage{multirow}
\usepackage{xparse}
% a command for doing the repetitive things:
% 1. open center, 2. choose the font size,
% 3. start the tabular, 4. print the header
% Next the command processes the argument and
% ends the job
\NewDocumentCommand{\chungeltable}{m}
{%
\begin{center}\scriptsize
\begin{tabular}{ | p{4cm} | p{3cm} | *{13}{l|} p{3cm} | }
\hline
\multirow{3}{*}{Case name} &
\multirow{3}{*}{Description} &
\multicolumn{14}{c|}{Further Details} \\
\cline{3-16}
& & \multicolumn{3}{c|}{O} & \multicolumn{3}{c|}{A} & \multicolumn{2}{c|}{F} &
\multicolumn{3}{c|}{B} & \multirow{2}{*}{G} & \multirow{2}{*}{C} & \multirow{2}{*}{Other} \\
\cline{3-13}
& & 1 & 2 & 3 & 4 & 5 & 6 & 7 & 8 & 9 & 10 & 11 & & & \\ \hline
\chungeltablebody{#1}
\end{tabular}
\end{center}
}
\ExplSyntaxOn
\NewDocumentCommand{\chungeltablebody}{m}
{
% clear the variable that will contain the table body
\tl_clear:N \l_tmpa_tl
% for each item in the comma separated list
\clist_map_inline:nn { #1 }
{
% add the item followed by the necessary number of &'s
% to the variable containing the table body
\tl_put_right:Nn \l_tmpa_tl { ##1 & & & & & & & & & & & & & & & \\\hline }
}
% deliver the table body
\tl_use:N \l_tmpa_tl
}
\ExplSyntaxOff
\begin{document}
\chungeltable{a,b}
\end{document}
キーは です\clist_map_inline:nn
。最初の引数の各項目は、#1
2 番目の引数のコードとして渡されます。
定義の本体にいるので、#1
は になります。これは、が定義しているマクロの実際の引数である##1
ためです。#1