-1

If I've the following code that is autogenerated (adapted here for sharing) from another tool, that I cannot modify,:

\documentclass[oneside,a4paper]{book}

\usepackage{longtable}
\usepackage{tablefootnote}
\usepackage{booktabs}

\begin{document}

\begin{longtable}[]{@{}ll@{}}
\toprule
\begin{minipage}[b]{0.29\columnwidth} Value\strut \end{minipage} & \begin{minipage}[b]{0.65\columnwidth} Description\strut \end{minipage}\tabularnewline
\midrule
\endhead
\begin{minipage}[t]{0.29\columnwidth} \textbf{Value 1}\strut \end{minipage} & \begin{minipage}[t]{0.65\columnwidth} Some details for 1\strut \end{minipage}\tabularnewline
\begin{minipage}[t]{0.29\columnwidth} \textbf{Value 2}\strut \end{minipage} & \begin{minipage}[t]{0.65\columnwidth} Some details for 2\strut \end{minipage}\tabularnewline
\bottomrule
\end{longtable}
\end{document}

Is there a way that I can easily replace \tabularnewline with \hline in the latex preamble somehow?

I've tried things like this, but with no success. Relax seems to work, but the switch to hline does not.

\let\toprule\hline
\let\midrule\hline
\let\bottomrule\relax
\let\tabularnewline\hline

I'm basically trying to get horizontal lines at the top, bottom and after every row, so that it ends up looking like this (done using manual search and replace as a proof of concept):

Aim

Is this possible?

6
  • 1
    First of all always post a full minimal example that others can copy ans test as is. People are more likely to help if they have code they can work with (without having to add anything). Note that you cannot just change \tabularnewline to \hline as that is not what dis macro does. You might be able to redefine it in to something silimar to \\\hline. Note that I would still use \toprule and friends as they have better spacing than \hline.
    – daleif
    Jun 15, 2022 at 10:55
  • Welcome to TeX.SE!
    – Zarko
    Jun 15, 2022 at 10:55
  • Thanks daleif, I appreciate the constructive comments on the posting. I'll make some tweaks to it now for clarity
    – BRH
    Jun 15, 2022 at 11:31
  • All minipages in your table are superfluous ( they are just clutter). The very same result you can get with with properly selected column specifiers. For other suggestion see my answer.
    – Zarko
    Jun 15, 2022 at 11:55
  • I don't have control over the LaTeX, which is why I was trying to see if there was a simple overloading of command that could be used. The flow is Source1->LaTeX->PDF. I am only able to change Source1 or add macros/commands into LaTeX, not re-write LaTeX
    – BRH
    Jun 15, 2022 at 12:44

2 Answers 2

0

The tabular environment defines tabularnewline to some appropriate value when the environment starts, so if you try to define it beforehand it will get overridden when the environment starts anyway.

This is doing literally what the OP asks for (without regarding whether it's a good idea or not), replacing each tabularnewline with \\ \hline. (note that then the middle rule and the bottomrule get duplicated. It's the readers's exercise to handle them.)

Remark

  • \__a violates expl3's naming style. Fix yourself if you want.
  • Documentation can be found in texdoc interface3 / texdoc xparse
  • Synctex is lost, and verbatim cannot be used inside the environment. These can be fixed, but it's a bit difficult.
  • For simplicity, the environment grabs the initial om arguments inside the #1. It doesn't really matter in this case, but watch out if you modify the code.
%! TEX program = lualatex
\documentclass[oneside,a4paper]{book}

\usepackage{longtable}
\usepackage{tablefootnote}
\usepackage{booktabs}

\ExplSyntaxOn

% ======== backup the old environment
\let    \oldlongtable    \longtable
\let \endoldlongtable \endlongtable


% ======== override it
\RenewDocumentEnvironment {longtable} {+b} {
  \tl_set:Nn \__a {#1}
  \regex_replace_all:nnN {\c{tabularnewline}} {\c{ \\ }\c{hline}} \__a
  \exp_args:NV \__printtable:n \__a
}{}

\cs_set_protected:Npn \__printtable:n #1 {
  \begin{oldlongtable} #1 \end{oldlongtable}
}


\ExplSyntaxOff


\begin{document}

\begin{longtable}[]{@{}ll@{}}
\toprule
\begin{minipage}[b]{0.29\columnwidth} Value\strut \end{minipage} & \begin{minipage}[b]{0.65\columnwidth} Description\strut \end{minipage}\tabularnewline
\midrule
\endhead
\begin{minipage}[t]{0.29\columnwidth} \textbf{Value 1}\strut \end{minipage} & \begin{minipage}[t]{0.65\columnwidth} Some details for 1\strut \end{minipage}\tabularnewline
\begin{minipage}[t]{0.29\columnwidth} \textbf{Value 2}\strut \end{minipage} & \begin{minipage}[t]{0.65\columnwidth} Some details for 2\strut \end{minipage}\tabularnewline
\bottomrule
\end{longtable}
\end{document}
1
  • Thank you, that's really helpful
    – BRH
    Jun 15, 2022 at 14:22
1

I would not experimenting what you try to do. Better and far more simple is use tabularray package. Using it your code will become very short and clear:

\documentclass{article}
\usepackage{tabularray}



\begin{document}
\begin{longtblr}[
caption = Table title
                ]{hline{1,Z}=0.8pt, hline{2}=0.4pt, hline{3-Z}=solid,
                 colspec = {@{} X[0.3,l, font=\bfseries]X[0.7,c] @{}},
                 row{1}={font=\bfseries},
                 rowhead=1
                 }
Value   &   Description             \\
Value 1 &   Some details for 1      \\
Value 2 &   Some details for 2      \\
Value 3 &   Some details for 3      \\
Value 4 &   Some details for 4      \\
\end{longtblr}
\end{document}

enter image description here

However, I would not draw lines after each row. Also is not clear, if texts in the second column cells have only one line. Ih have more, than centering it may not be the best choice. For example:

\documentclass{article}
\usepackage{tabularray}

\usepackage{lipsum}


\begin{document}
\begin{longtblr}[
caption = Table title
                ]{hline{1,Z}=0.8pt, hline{2}=0.4pt, 
                 colspec = {@{} X[0.3,l, font=\bfseries]X[0.7,j] @{}},
                 row{1}={font=\bfseries},
                 row{2-Y}={rowsep=3pt},
                 rowhead=1
                 }
Value   &   Description     \\
Value 1 &   \lipsum[1]     \\
Value 2 &   \lipsum[2]     \\
Value 3 &   \lipsum[11]     \\
Value 4 &   \lipsum[66]     \\
\end{longtblr}
\end{document}

looks nice regardless that text in the second column is not centered.

enter image description here

3
  • Thank you Zarko, I agree that the output and likewise the source here look good. The background here is that there are certain tables that absolutely do require horizontal lines in order to make the content clearer. Note that the LaTeX above is autogenerated and that part I'm unable to change unfortunately as it's in the middle of a flow that doesn't support processing in the middle
    – BRH
    Jun 15, 2022 at 11:29
  • @BRH, I show you how to simple draw horizontal lines in table (first example) . BTW, So you can use both suggestion. BTW, I strongly disagree with you about horizontal lines. You may look wiert.me/2014/04/03/…
    – Zarko
    Jun 15, 2022 at 11:33
  • I cannot change the source though. All I can do is put some LaTeX macros in place. Agree as well on your link absolutely - for a very specific table lines are needed. There was feedback saying they were confusing without
    – BRH
    Jun 15, 2022 at 11:45

You must log in to answer this question.

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