2

I found this very useful macro to add rows to a table (while defining the rows externally from the table): Command to continually add to table with a command/macro

In my case the macro has only some changes, it works fine:

\documentclass{article}
\usepackage{longtable}
\newcommand\PutLongtableRows{}
\makeatletter
\newcommand\AddLongtableRow[1]{%
    \g@addto@macro\PutLongtableRows{\@gobble}%
    \@for\tmp:=#1\do{%
        \expandafter\g@addto@macro\expandafter\PutLongtableRows
        \expandafter{\expandafter&\tmp}%
    }%
    \g@addto@macro\PutLongtableRows{\\*\nobreakhline}%
}
\makeatother

\begin{document}

\AddLongtableRow{5.9,5.8,R2C3,R2,3C}
\AddLongtableRow{R2C1,RCC2C2,R2C3,R2,3C}

\begin{longtable}{p{2cm}|p{3cm}|p{3cm}|p{2.5cm}|p{2.5cm}}
    \caption{Blabla}
    \\*\nobreakhline\nobreakhline
    \PutLongTableRows
    \nobreakhline
\end{longtable}
\end{document}

Now I face the problem of the comma separation. I need to change the separator since numbers in our report have a comma. So the following will definitely lead to a wrong table:

\AddLongtableRow{5,9,5,8,R2C3,R2,3C}

If i could change the separator i. e. to semicolon ; it could work like this:

\AddLongtableRow{5,9;5,8;R2C3;R2;3C}

How to change the comma separator to another one? If I leave the & in the macro it will not work either even after removing the for loop... Appreciate your help (if possible without additional packages).

3
  • Welcome to the site. Please provide a minimum working example that compiles. – Steven B. Segletes Feb 11 '20 at 13:19
  • 2
    Can't you simply put braces around the number? \AddLongtableRow{{5,5},9,} should work fine. – Ulrike Fischer Feb 11 '20 at 13:56
  • Yes - that's indeed the most simple way to do it. Thank you! – konsens Feb 13 '20 at 12:11
2

Here, I use listofitems to achieve a different parsing character, so that commas can now appear in the table data.

EDITED to make the row separator optionally specified as an argument to \AddLongtableRow. Default is , but can be changed, as in row 3 of the MWE.

\documentclass{article}
\usepackage{longtable,listofitems}
\newcommand\PutLongtableRows{}
\newcommand\nobreakhline{\hline}
\makeatletter
\newcommand\AddLongtableRow[2][,]{%
    \g@addto@macro\PutLongtableRows{\@gobble}%
    \setsepchar{#1}% LISTOFITEMS SEP CHAR
    \readlist\z{#2}% READ THE LIST INTO \z
    \foreachitem\tmp\in\z{% LISTOFITEMS DO LOOP
        \expandafter\g@addto@macro\expandafter\PutLongtableRows
        \expandafter{\expandafter&\tmp}%
    }%
    \g@addto@macro\PutLongtableRows{\\*\nobreakhline}%
}
\makeatother

\AddLongtableRow{5.9,5.8,R2C3,R2,3C}
\AddLongtableRow{R2C1,RCC2C2,R2C3,R2,3C}
\AddLongtableRow[;]{5,9;5,8;R2C3;R2;3C}
\begin{longtable}{p{2cm}|p{3cm}|p{3cm}|p{2.5cm}|p{2.5cm}}
    \caption{Blabla}
    \\*\nobreakhline\nobreakhline
    \PutLongtableRows
    \nobreakhline
\end{longtable}

\end{document}

enter image description here

7
  • Thank you! There is another little question: As you see the first two lines and the last two lines are falling together. When using \hline instead of \nobreakhline it shows up two separated lines. How to manage this with \nobreakhline (see also: tex.stackexchange.com/questions/528814/…) – konsens Mar 10 '20 at 10:00
  • @konsens I would note that achieving double-broken lines is not achieved by anything in the \AddLongTableRow macro, but rather by changing the \nobreakhlines to \hlines that have been manually added to the longtable. So I am not exactly clear what you mean by "manage this"? – Steven B. Segletes Mar 10 '20 at 10:14
  • Yes, it's not about \AddLongTableRow anymore. \hline was modified to be \nobreakhline. However I want that two \nobreakhlines behave the same (apart from pagebreak behavieour) as two \hlines. But they don't. Two \nobreakhlines are falling togehter two \hlines are showing up as two separated \hlines (see question: tex.stackexchange.com/questions/528814/…). Any idea? – konsens Mar 10 '20 at 10:25
  • @konsens That is the purpose (and the name nobreakhline): to act differently than \hline. They only act the same if they occur as singles. When occurring in multiples, each \hline makes a new line, whereas each \nobreakhline will never make a new line. Your only alternative is to use something like \myhline inside your table, where you either \let\myhline\hline or else \let\myhline\nobreakhline in advance. – Steven B. Segletes Mar 10 '20 at 10:30
  • @konsens There is no doubt I have little experience with \nobreakhline. Someone like David Carlisle (who probably wrote the macro code) would be a better resource than I on this follow-up question. Good luck! – Steven B. Segletes Mar 10 '20 at 10:41
3

You can use a simpler expl3 implementation, which allows to change the delimiter at calling time (default is a comma).

\documentclass{article}
\usepackage{longtable,colortbl}
\usepackage{xparse}

% from https://tex.stackexchange.com/a/107893/4427
\makeatletter
\def\nobreakhline{%
\noalign{\ifnum0=`}\fi
 \penalty\@M
\futurelet\@let@token\LT@@nobreakhline}
\def\LT@@nobreakhline{%
\ifx\LT@next\hline
  \global\let\LT@next\@gobble
 \ifx\CT@drsc@\relax
   \gdef\CT@LT@sep{%
     \noalign{\penalty\@M\vskip\doublerulesep}}%
 \else
   \gdef\CT@LT@sep{%
     \multispan\LT@cols{%
       \CT@drsc@\leaders\hrule\@height\doublerulesep\hfill}\cr}%
 \fi
\else
 \global\let\LT@next\empty
 \gdef\CT@LT@sep{%
   \noalign{\penalty\@M\vskip-\arrayrulewidth}}%
\fi
\ifnum0=`{\fi}%
\multispan\LT@cols
 {\CT@arc@\leaders\hrule\@height\arrayrulewidth\hfill}\cr
\CT@LT@sep
\multispan\LT@cols
 {\CT@arc@\leaders\hrule\@height\arrayrulewidth\hfill}\cr
\noalign{\penalty\@M}%
\LT@next}
\makeatother

\ExplSyntaxOn

\tl_new:N \g_konsens_tablerow_tl
\seq_new:N \l__konsens_tablerow_seq

\NewDocumentCommand\AddLongtableRow{O{,}m}
 {
  \seq_set_split:Nnn \l__konsens_tablerow_seq { #1 } { #2 }
  \tl_gput_right:Nx \g_konsens_tablerow_tl
   {
    \seq_use:Nn \l__konsens_tablerow_seq { & } 
    \exp_not:n { \\*\nobreakhline} }
 }
\NewDocumentCommand{\PutLongTableRows}{}
 {
  \tl_use:N \g_konsens_tablerow_tl
 }

\ExplSyntaxOff

\begin{document}

\AddLongtableRow[;]{5,9;5,8;R2C3;R2;3C}
\AddLongtableRow{R2C1,RCC2C2,R2C3,R2,3C}

\begin{longtable}{p{2cm}|p{3cm}|p{3cm}|p{2.5cm}|p{2.5cm}}
    \caption{Blabla}
    \\*\nobreakhline\nobreakhline
    \PutLongTableRows
    \nobreakhline
\end{longtable}

\end{document}

enter image description here

2

Welcome to TeX.SE! Here is a small variation on egreg's nice answer (+1). The user-visible differences are:

  • \PutLongTableRows allows you to choose the separator between rows. For instance, using \PutLongTableRows[\\] won't insert any rule between consecutive rows (the optional argument defaults to \\*\nobreakhline, since this seems to be your preference).

  • \PutLongTableRows doesn't append the separator after the last row. This way, you can use whatever seems most appropriate in this place—the “finish” doesn't have to be identical as the separator between two adjacent rows.

\documentclass{article}
\usepackage{longtable}
\usepackage{colortbl}
\usepackage{xparse}

% From <https://tex.stackexchange.com/a/107893/4427>
\makeatletter
\def\nobreakhline{%
\noalign{\ifnum0=`}\fi
 \penalty\@M
\futurelet\@let@token\LT@@nobreakhline}
\def\LT@@nobreakhline{%
\ifx\LT@next\hline
  \global\let\LT@next\@gobble
 \ifx\CT@drsc@\relax
   \gdef\CT@LT@sep{%
     \noalign{\penalty\@M\vskip\doublerulesep}}%
 \else
   \gdef\CT@LT@sep{%
     \multispan\LT@cols{%
       \CT@drsc@\leaders\hrule\@height\doublerulesep\hfill}\cr}%
 \fi
\else
 \global\let\LT@next\empty
 \gdef\CT@LT@sep{%
   \noalign{\penalty\@M\vskip-\arrayrulewidth}}%
\fi
\ifnum0=`{\fi}%
\multispan\LT@cols
 {\CT@arc@\leaders\hrule\@height\arrayrulewidth\hfill}\cr
\CT@LT@sep
\multispan\LT@cols
 {\CT@arc@\leaders\hrule\@height\arrayrulewidth\hfill}\cr
\noalign{\penalty\@M}%
\LT@next}
\makeatother

\ExplSyntaxOn

\seq_new:N \g_konsens_tablerows_seq
\seq_new:N \l__konsens_tablerow_seq

% Based on egreg's answer: <https://tex.stackexchange.com/a/527939/73317>
\NewDocumentCommand \AddLongtableRow { O{,} m }
 {
   \seq_set_split:Nnn \l__konsens_tablerow_seq {#1} {#2}
   \seq_gput_right:Nx \g_konsens_tablerows_seq
     { \seq_use:Nn \l__konsens_tablerow_seq { & } }
 }

\NewDocumentCommand \PutLongTableRows { O{\\*\nobreakhline} }
  {
    \seq_use:Nn \g_konsens_tablerows_seq {#1}
  }

\ExplSyntaxOff

\begin{document}

\AddLongtableRow[;]{5,9;5,8;R2C3;R2;3C}
\AddLongtableRow{R2C1,RCC2C2,R2C3,R2,3C}

\begin{longtable}{p{2cm}|p{3cm}|p{3cm}|p{2.5cm}|p{2.5cm}}
    \caption{Blabla}
    \\*\nobreakhline\nobreakhline
    \PutLongTableRows
    \\*\nobreakhline
    \nobreakhline
\end{longtable}

\begin{longtable}{p{2cm}|p{3cm}|p{3cm}|p{2.5cm}|p{2.5cm}}
    \caption{Blabla}
    \\*\nobreakhline\nobreakhline
    \PutLongTableRows[\\]
    \\*\nobreakhline
    \nobreakhline
\end{longtable}

\end{document}

enter image description here

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.