3

I have a simple table:

\documentclass{article}
\pagestyle{empty}
\begin{document}
\begin{table}[ht]
  \centering
  \begin{tabular}{c c}
    \hline\hline
    x & y \\ [0.5ex]
    \hline
    1 & 4 \\
    2 & 6 \\
    3 & 8 \\
    [1ex]
    \hline
  \end{tabular}
\end{table}
\end{document}

It looks like this:

simple table

I would like to replicate this with datatool, keeping the same horizontal lines and vertical space:

\documentclass{article}
\usepackage{datatool}
\usepackage{filecontents}
\pagestyle{empty}
\begin{filecontents*}{input.csv}
x,y
1,4
2,6
3,8
\end{filecontents*}
\begin{document}
\DTLloaddb{mytable}{input.csv}
\begin{table}[ht]
  \centering
  \begin{tabular}{c c}
    \hline\hline
    x & y \\ [0.5ex]
    \hline
    \DTLforeach{mytable}
    {\x=x,\y=y}
    {
      \DTLiffirstrow{}{\\}
      \x & \y
    }
    \\
    [1ex]
    \hline
  \end{tabular}
\end{table}
\end{document}

This is almost the same, but the 1 is misaligned horizontally:

misaligned table

How do I fix this?

  • Avoid \pagenumbering{gobble}. If you don't want page numbers, use \pagestyle{empty}. – egreg Nov 9 '17 at 9:55
  • Alright, fixed it. – Nathaniel M. Beaver Nov 9 '17 at 14:25
4

Spot the differences:

\begin{filecontents*}{\jobname.csv}
x,y
1,4
2,6
3,8
\end{filecontents*}

\documentclass{article}
\usepackage{datatool}

\begin{document}
\DTLloaddb{mytable}{\jobname.csv}

\begin{table}[ht]
  \centering
  \begin{tabular}{c c}
    \hline\hline
    x & y \\ [0.5ex]
    \hline
    \DTLforeach{mytable}
    {\x=x,\y=y}
    {%
      \DTLiffirstrow{}{\\}%
      \x & \y
    }
    \\[1ex]
    \hline
  \end{tabular}
\end{table}
\end{document}

enter image description here

No, the position of filecontents and the file name are irrelevant. Look for %.

You added two spaces with the end-of-lines; the first one is not really relevant, because LaTeX will ignore it, but the one after \DTLiffirstrow{}{\\} doesn't get ignored.

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.