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}
up vote 2 down vote accepted

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}

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

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}
  • 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. – Peter Grill Aug 9 at 23:09

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

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