SalysBruoga - 1 year ago 211
LaTeX Question

# LaTeX custom command with many arguments

I'm working on a document, where I'll be using a lot of tables of the same structure, so I decided to create custom commands to make working with the tables easier. I have written commands such as:

\newcommand{\reqV}[7]
{
\multirow{5}{*}{#1} & \subReq{#1.1}{#3}   & \multirow{5}{*}{#2} \\
\cline{2-3}
& \subReq{#1.2}{#4}   &                     \\
\cline{2-3}
& \subReq{#1.3}{#5}   &                     \\
\cline{2-3}
& \subReq{#1.4}{#6}   &                     \\
\cline{2-3}
& \subReq{#1.5}{#7}   &                     \\
}


for inserting an item with 5 subitems into the table. I have similar commands
\reqI
,
\reqII
and so on. However I ran into a problem. The maximum number of arguments allowed is 9, so I can only have an item with at most 7 subitems, which might not be enough for me. Is there any way to "outplay the system" here to allow more arguments? Or maybe there's a more clever way of doing this?

The idea is that I want to have some code in the leftmost cell. That cell should extend over some number of rows. I also want to have a single word in the rightmost cell, which should also extend over the same number of rows. All the rows in between are counted as subitems and should have a code (as shown in the example) and a description (denoted by letters A to E here, but the text might be much longer).

The general approach of creating macros that can handle more than nine arguments is discussed in How to define a command that takes more than 9 arguments. However, here is an alternative approach.

\req is defined to take only three arguments. The first for the left column, the second for the middle column text and the third for the right column. The column entries are centred vertically, and each row within the middle column has an automated counter that steps with every row \\:

\documentclass{article}

\usepackage{tabularx}
\renewcommand{\tabularxcolumn}[1]{ m{#1} }
\newcolumntype{M}[1]{ >{\centering\arraybackslash} m{#1} }

\newcounter{req}
\newcommand{\req}[3]{%
\setcounter{req}{1}%
\par\noindent
\begin{tabularx}{\linewidth}{ | M{5em} X M{7em} | }
\hline
#1 &
{\let\oldbackslash\\
\renewcommand{\tabularxcolumn}[1]{ p{##1} }%
\begin{tabularx}{\linewidth}{ | @{\hspace{\tabcolsep}#1.\thereq\hspace{\tabcolsep}} | X | }
\global\def\\{\stepcounter{req}\oldbackslash \hline}
#2
\end{tabularx}%
\global\let\\\oldbackslash} &
#3 \\
\hline
\end{tabularx}
\par
}

\begin{document}

\req{IR5}{A \\ B \\ C \\ D}{Mandatory}

\req{IR6}{%
Lorem ipsum dolor sit amet, consectetur adipiscing elit. \\
Pellentesque eget nisi non tortor tincidunt porttitor. \\
Aliquam eget sem arcu. \\
Suspendisse potenti. \\
Nullam pellentesque sapien vel leo consequat facilisis. \\
Aliquam pretium convallis arcu, ut volutpat massa pellentesque quis. \\
Praesent dignissim facilisis risus ac dignissim. \\
Interdum et malesuada fames ac ante ipsum primis in faucibus.}{Optional}

\end{document}

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download