我是 Latex 的初学者,我正在尝试在 Latex 中构建一个具有多个参数的宏。到目前为止,我已经成功编写了一个接受多个参数的宏(借助本网站):
\documentclass{article}
\usepackage{tabularx}
\newcommand*{\arrayGeneration}[1]{%
\begin{tabularx}{\textwidth}{|X|X|X|X|X|}
\hline
Col 1 & Col 2 & Col 3 & Col 4 & Col5 \tabularnewline \hline
\lineScanFive#1\relax\relax\relax\relax\relax
\end{tabularx}
}
\newcommand{\lineScanFive}[5]{%
\ifx\relax #1 \empty
\else
#1 & #2 & #3 & #4 & #5 \\ \hline
\expandafter\lineScanFive
\fi
}
\begin{document}
\arrayGeneration{
{elt1}{elt2}{elt3}{elt4}{elt5}
{elt6}{elt7}{elt8}{elt9}{elt10}
}
\end{document}
但是现在,我想让宏 arrayGeneration 使用之前在另一个宏中定义的参数。但是,我的新 Latex 文件无法编译(\lineScanFive 的参数有一个额外的 }):
\documentclass{article}
\usepackage{tabularx}
\newcommand*{\arrayGeneration}{%
\begin{tabularx}{\textwidth}{|X|X|X|X|X|}
\hline
Col 1 & Col 2 & Col 3 & Col 4 & Col5 \tabularnewline \hline
\lineScanFive{storeParameter}\relax\relax\relax\relax\relax
\end{tabularx}
}
\newcommand{\lineScanFive}[5]{%
\ifx\relax #1 \empty
\else
#1 & #2 & #3 & #4 & #5 \\ \hline
\expandafter\lineScanFive
\fi
}
\newcommand\storeParameter[1]{\newcommand\generationParam{#1}}
\begin{document}
\storeParameter{{elt1}{elt2}{elt3}{elt4}{elt5}
{elt6}{elt7}{elt8}{elt9}{elt10}}
\arrayGeneration
\end{document}
谢谢 !
答案1
您可以按照如下方式操作
\documentclass{article}
\usepackage{tabularx}
\newcommand*{\arrayGeneration}{%
\noindent\begin{tabularx}{\textwidth}{|X|X|X|X|X|}
\hline
Col 1 & Col 2 & Col 3 & Col 4 & Col5 \tabularnewline \hline
\expandafter\lineScanFive\generationParam\relax\relax\relax\relax\relax
\end{tabularx}
}
\newcommand{\lineScanFive}[5]{%
\ifx\relax#1%
\else
#1 & #2 & #3 & #4 & #5 \\ \hline
\expandafter\lineScanFive
\fi
}
\newcommand{\generationParam}{}
\newcommand\storeParameter[1]{\renewcommand\generationParam{#1}}
\begin{document}
\storeParameter{{elt1}{elt2}{elt3}{elt4}{elt5}
{elt6}{elt7}{elt8}{elt9}{elt10}}
\arrayGeneration
\end{document}
但我认为与直接输入相比没有什么实际优势。