2

I would like to insert a cmidrule when I have an empty line in a table produced by the datatool package and also use \bottomrule. Both of these yield excess vertical space above the line:

enter image description here

How do I eliminate these two instances of excess vertical space?

Code:

\documentclass{article}
\usepackage{datatool}
\usepackage{booktabs}

\usepackage{filecontents}
\begin{filecontents*}{foo.dat}
    Line1,   AAAA
    Line2,   BBBB
    ,
    Line4,   DDDD
    Line5,   EEEE
\end{filecontents*}

\newcommand{\PrintDTLTable}[1]{%
    % #1 = database to search
    \begin{tabular}{c c}\toprule
        Label & Cost  \\\cmidrule{1-2}
    \DTLforeach{#1}{%
        \RowID=RowID,%
        \Label=Label%
    }{%
        \DTLifnullorempty{\RowID}{%
            \\%[-\baselineskip]
            %\vspace*{-\baselineskip}%
            \cmidrule(lr){1-2}%
        }{%
            \RowID & \Label \\
        }%
    }%
    \\\bottomrule
    \end{tabular}
}%

\begin{document}
\DTLloaddb[noheader,keys={RowID,Label}]{myDB}{foo.dat}

\PrintDTLTable{myDB}
\end{document}

3 Answers 3

2

Within a tabular, \baselineskip is set to 0pt, but you can use \normalbaselineskip to correct for the additional spaces:

enter image description here

\documentclass{article}

\usepackage{datatool}
\usepackage{booktabs}

\usepackage{filecontents}
\begin{filecontents*}{foo.dat}
  Line1,   AAAA
  Line2,   BBBB
  ,
  Line4,   DDDD
  Line5,   EEEE
\end{filecontents*}

\newcommand{\PrintDTLTable}[1]{%
  % #1 = database to search
  \begin{tabular}{c c}\toprule
    Label & Cost  \\\cmidrule{1-2}
    \DTLforeach{#1}{%
      \RowID=RowID,%
      \Label=Label%
    }{%
      \DTLifnullorempty{\RowID}{%
        \\[-\normalbaselineskip]
        \cmidrule(lr){1-2}%
      }{%
        \RowID & \Label \\
      }%
    }%
    \\[-\normalbaselineskip]
    \bottomrule
  \end{tabular}
}%

\begin{document}
\DTLloaddb[noheader,keys={RowID,Label}]{myDB}{foo.dat}

\PrintDTLTable{myDB}
\end{document}
2

As usual, it's better to build in advance the table body and deliver it at once.

\begin{filecontents*}{\jobname.dat}
    Line1,   AAAA
    Line2,   BBBB
    ,
    Line4,   DDDD
    Line5,   EEEE
\end{filecontents*}

\documentclass{article}
\usepackage{datatool}
\usepackage{booktabs}

\newcommand{\PrintDTLTable}[1]{%
  % #1 = database to search
  \def\tabledata{}%
  \DTLforeach{#1}{%
    \RowID=RowID,%
    \Label=Label%
  }{%
    \DTLifnullorempty{\RowID}{%
      \edef\tabledata{\expandonce{\tabledata}\noexpand\cmidrule(lr){1-2}}
    }{%
      \edef\tabledata{%
        \expandonce{\tabledata}%
        \expandonce{\RowID} &
        \expandonce{\Label} \noexpand\\%
     }%
   }%
 }%
 \begin{tabular}{c c}\toprule
 Label & Cost  \\\cmidrule{1-2}
 \tabledata
 \bottomrule
 \end{tabular}
}

\begin{document}
\DTLloaddb[noheader,keys={RowID,Label}]{myDB}{\jobname.dat}

\PrintDTLTable{myDB}
\end{document}

enter image description here

1

A workaround would be to set negative vertical space before the middle and bottom rules with [-2.75ex]

enter image description here

\documentclass{article}
\usepackage{datatool}
\usepackage{booktabs}

\usepackage{filecontents}
\begin{filecontents*}{foo.dat}
    Line1,   AAAA \\
    Line2,   BBBB \\ [-2.75ex]
    ,
    Line4,   DDDD \\
    Line5,   EEEE \\ [-2.75ex]
\end{filecontents*}

\newcommand{\PrintDTLTable}[1]{%
    % #1 = database to search
    \begin{tabular}{c c}\toprule
        Label & Cost  \\\cmidrule{1-2}
    \DTLforeach{#1}{%
        \RowID=RowID,%
        \Label=Label%
    }{%
        \DTLifnullorempty{\RowID}{%
            \\%[-\baselineskip]
            %\vspace*{-\baselineskip}%
            \cmidrule(lr){1-2}%
        }{%
            \RowID & \Label  
        }%
    }%
    \\\bottomrule
    \end{tabular}
}%

\begin{document}
\DTLloaddb[noheader,keys={RowID,Label}]{myDB}{foo.dat}

\PrintDTLTable{myDB}
\end{document}
1
  • Oppss. Did not realize that I had the `\\` in the actual data (I cut and used the code from another answer). Have corrected the question. BUT, I don't think it is a good idea to edit the data for formatting related issues. Aug 9, 2018 at 23:09

You must log in to answer this question.

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