2

My question is same as this question but with this different that I need an \hline after every row in \tabledata. How can I do it?

Currently my code is as below:

\documentclass{article}
\usepackage{etoolbox}
\usepackage{tikz}
\makeatletter
\newcommand{\myTable}[1]{%
    \def\tabledata{}% reset \tabledata
    \foreach \lhs/\rhs in {#1} {% build table data from #1  
        \protected@xappto\tabledata{\textbf{\lhs} & \rhs \\}
    }%
    \begin{tabular}{p{0.35\textwidth}|p{0.65\textwidth}}
        \hline
        Headline 1 & Headline 2 \\ \hline
        \tabledata \hline
    \end{tabular}
}
\makeatother

\begin{document}
   \myTable{{Title 1}/{Description 1}, {Title 2}/{Description 2}}
\end{document}

If I add \hline simply:

\documentclass{article}
\usepackage{etoolbox}
\usepackage{tikz}
\makeatletter
\newcommand{\myTable}[1]{%
    \def\tabledata{}% reset \tabledata
    \foreach \lhs/\rhs in {#1} {% build table data from #1  
        \protected@xappto\tabledata{\textbf{\lhs} & \rhs \\ \hline}
    }%
    \begin{tabular}{p{0.35\textwidth}|p{0.65\textwidth}}
        \hline
        Headline 1 & Headline 2 \\ \hline
        \tabledata \hline
    \end{tabular}
}
\makeatother

\begin{document}
   \myTable{{Title 1}/{Description 1}, {Title 2}/{Description 2}}
\end{document}

Error:

Missing control sequence inserted.

Log File:

<inserted text> 
                \inaccessible 
l.19 ...Description 1}, {Title 2}/{Description 2}}
                                                  
Please don't say `\def cs{...}', say `\def\cs{...}'.
I've inserted an inaccessible control sequence so that your
definition will be completed without mixing me up too badly.
You can recover graciously from this error, if you're
careful; see exercise 27.2 in The TeXbook.

And also it produces following result: enter image description here

1

2 Answers 2

3

Add \noexpand before \hline.

\documentclass{article}
\usepackage{etoolbox}
\usepackage{tikz}
\makeatletter
\newcommand{\myTable}[1]{%
    \def\tabledata{}% reset \tabledata
    \foreach \lhs/\rhs in {#1} {% build table data from #1  
        \protected@xappto\tabledata{\textbf{\lhs} & \rhs \\\noexpand\hline}
    }%
    \begin{tabular}{p{0.35\textwidth}|p{0.65\textwidth}}
        \hline
        Headline 1 & Headline 2 \\ \hline
        \tabledata %\hline %don't need
    \end{tabular}
}
\makeatother

\begin{document}
   \myTable{{Title 1}/{Description 1}, {Title 2}/{Description 2},{Title 3}/{Description 3}}
\end{document}

enter image description here

3

Here is a LaTeX3 version without tikz:

\documentclass{article}

\ExplSyntaxOn
\cs_new:Npn \my_print_row:n #1 {
    \tl_set:Nn \l_tmpa_tl {#1}
    \regex_replace_all:nnN {(.+)/(.+)} {\c{textbf}{\1} & \2} \l_tmpa_tl
    \tl_use:N \l_tmpa_tl \\ \hline
}

\NewDocumentCommand {\myTable} { >{\SplitList{,}}m } {
    \begin{tabular}{p{0.35\textwidth}|p{0.65\textwidth}}
        \hline
        Headline 1 & Headline 2 \\ \hline
        \ProcessList{#1}{\my_print_row:n}
    \end{tabular}
}
\ExplSyntaxOff

\begin{document}
    \myTable{Title 1/Description 1,Title 2/Description 2,Title 3/Description 3}
\end{document}

enter image description here

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .