# How to use \hline in \edef macro to generate table

does someone know how to add \hline to a macro which is used to generate a table?

My MWE

\documentclass{article}

\RequirePackage{tabularx}

\makeatletter
\newcounter{appdocs}
\setcounter{appdocs}{1}
\newcommand{\appdocsCnt}[1]{%
\theappdocs% Print counter
\refstepcounter{appdocs}\label{#1}}% Mark with label

\newcommand{\applicabledocumententries}{}
\newcommand{\applicabledocument}[3]{%
\protected@xdef\applicabledocumententries{\applicabledocumententries \protect\appdocsCnt{#1} & #2 & #3 \protect\\}}

\newcommand{\applicabledocumentstable}{%
\begin{tabularx}{\textwidth}{p{2cm}|X|p{4cm}}%9.8
\hline
{\bfseries Ref.} & {\bfseries Title} & {\bfseries Reference and Issue} \\\hline
\applicabledocumententries
\hline
\end{tabularx}
}
\makeatother

\begin{document}

\applicabledocumentstable

\end{document}


which produces

But I would like to have \hline after each entry of the list. How to add \hline to macro expansion? might go into the correct direction, but I am not able to apply this to my example.

• Doesn't \protect\hline work (as you have done, e.g., with \\protect\\ )? Please note, the example should show the problem. I cannot see any problem with any of the \hline in your example. I even cannot find a \edef (only a \xdef). – Schweinebacke Jul 4 '17 at 15:13
• @Schweinebacke \xdef is equivalent to \global\edef. And \protect\hline does not work, it throws a ! Misplaced \noalign error – user2943030 Jul 4 '17 at 15:18
• I know the definition of \xdef. But you do not ask for a problem with \xdef. If you have a problem with \xdef you should ask for \xdef. If you title for \edef your example should use \edef. If you have a problem with adding \hline to the argument of a \xdef your example should show it. – Schweinebacke Jul 4 '17 at 15:22

You can use \DeclareRobustCommand to define a robust \hline or (as shown in the example) a tabular line end with \hline:

\documentclass{article}

\usepackage{tabularx}% Don't use \RequirePackage in document preamble after \documentclass

\makeatletter
\newcounter{appdocs}
\setcounter{appdocs}{1}
\newcommand{\appdocsCnt}[1]{%
\theappdocs% Print counter
\refstepcounter{appdocs}\label{#1}}% Mark with label

\newcommand{\applicabledocumententries}{}
\DeclareRobustCommand*{\tabularnewlinewithhline}{\\\hline}
\newcommand{\applicabledocument}[3]{%
\protected@xdef\applicabledocumententries{\applicabledocumententries \protect\appdocsCnt{#1} & #2 & #3 \tabularnewlinewithhline}}

\newcommand{\applicabledocumentstable}{%
\begin{tabularx}{\textwidth}{p{2cm}|X|p{4cm}}%9.8
\hline
{\bfseries Ref.} & {\bfseries Title} & {\bfseries Reference and Issue} \\\hline
\applicabledocumententries
\end{tabularx}
}
\makeatother

\begin{document}

\applicabledocumentstable

\end{document}


As an alternative you can expand and eliminate the \protect before using \applicabledocumententries:

\documentclass{article}

\usepackage{tabularx}
\makeatletter
\newcounter{appdocs}
\setcounter{appdocs}{1}
\newcommand{\appdocsCnt}[1]{%
\theappdocs% Print counter
\refstepcounter{appdocs}\label{#1}}% Mark with label

\newcommand{\applicabledocumententries}{}
\DeclareRobustCommand*{\tabularnewlinewithhline}{\\\hline}
\newcommand{\applicabledocument}[3]{%
\protected@xdef\applicabledocumententries{\applicabledocumententries \protect\appdocsCnt{#1} & #2 & #3 \protect\\\protect\hline}}

\newcommand{\applicabledocumentstable}{%
\begin{tabularx}{\textwidth}{p{2cm}|X|p{4cm}}%9.8
\hline
{\bfseries Ref.} & {\bfseries Title} & {\bfseries Reference and Issue}
\\\hline
\let\protect\noexpand
\edef\applicabledocumententries{\applicabledocumententries}%
\applicabledocumententries
\end{tabularx}
}
\makeatother

\begin{document}

\applicabledocumentstable

\end{document}

• \DeclareRobustCommand works like a charm too! Thanks for the help :) It shouldn't have any drawback to declare it robust? I think this is a pretty clean solution – user2943030 Jul 4 '17 at 15:32
• @user2943030: No drawback with the shown usage of the command. BTW: You could declare \appdocsCnt robust too and then remove the \protect before its usage. – Schweinebacke Jul 4 '17 at 15:37

Below I've inserted a macro \trules that is originally set to \relax (not expandable) as part of the \applicabledocumententries construction. \trules is then set to \hline as part of \applicabledocumentstable:

\documentclass{article}

\usepackage{tabularx}

\makeatletter
\newcounter{appdocs}
\newcommand{\appdocsCnt}[1]{%
\mbox{}\refstepcounter{appdocs}\label{#1}% Mark with label
\theappdocs% Print counter
}

\newcommand{\applicabledocumententries}{}
\let\trules\relax
\newcommand{\applicabledocument}[3]{%
\protected@xdef\applicabledocumententries{%
\applicabledocumententries \protect\appdocsCnt{#1} & #2 & #3 \protect\\ \trules}}
\makeatother

\newcommand{\applicabledocumentstable}{%
\let\trules\hline
\begin{tabularx}{\textwidth}{ p{2cm} | X | p{4cm} }
\hline
{\bfseries Ref.} & {\bfseries Title} & {\bfseries Reference and Issue} \\
\hline
\applicabledocumententries
\end{tabularx}
}
\makeatother

\begin{document}


The table looks far better without it though. Here is a version using booktabs: