1

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}
\renewcommand{\theappdocs}{AD\arabic{appdocs}}
\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}

\applicabledocument{ad:1}{myTexta}{someDate}
\applicabledocument{ad:2}{myTextb}{anotherDate}
\applicabledocument{ad:3}{myTextc}{randomDate}
\applicabledocumentstable

\end{document}

which produces

enter image description here

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.

  • 1
    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
2

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}
\renewcommand{\theappdocs}{AD\arabic{appdocs}}
\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}

\applicabledocument{ad:1}{myTexta}{someDate}
\applicabledocument{ad:2}{myTextb}{anotherDate}
\applicabledocument{ad:3}{myTextc}{randomDate}
\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}
\renewcommand{\theappdocs}{AD\arabic{appdocs}}
\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}

\applicabledocument{ad:1}{myTexta}{someDate}
\applicabledocument{ad:2}{myTextb}{anotherDate}
\applicabledocument{ad:3}{myTextc}{randomDate}
\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
1

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:

enter image description here

\documentclass{article}

\usepackage{tabularx}

\makeatletter
\newcounter{appdocs}
\renewcommand{\theappdocs}{AD\arabic{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}

\applicabledocument{ad:1}{myTexta}{someDate}
\applicabledocument{ad:2}{myTextb}{anotherDate}
\applicabledocument{ad:3}{myTextc}{randomDate}
\noindent
\applicabledocumentstable

\end{document}

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

enter image description here

  • @user2943030: Note also that I've changed the order of your counter representation/usage. Your references would have been wrong otherwise. – Werner Jul 4 '17 at 15:34

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.