1

I'm trying to replicate the folowing table in LaTeX with booktabs:

original

However, I have trouble making the vertical lines. Here's what I have so far:

\begin{tabularx}{\textwidth}{l | c c c c c c c c c c c c}
\toprule
    & \ce{NH4^{+}} & \ce{Na+} & \ce{K+} & \ce{Mg^{2+}} & \ce{Zn^{2+}} & \ce{Cu^{2+}} & \ce{Fe^{2+}} & \ce{Fe^{3+}} & \ce{Ca^{2+}} & \ce{Ba^{2+}} & \ce{Pb^{2+}} & \ce{Ag^{+}} \\
\midrule
    \ce{NO3^{-}} & L & L & L & L & L & L & L & L & L & L & L & L \\ \cmidrule(r){12-13}
    \ce{Cl-} & L & L & L & L & L & L & L & L & L & L & T & T \\ 
    \ce{Br-} & L & L & L & L & L & L & L & L & L & L & T & T \\ 
    \ce{I-} & L & L & L & L & L & L & L & L & L & L & T & T \\
\bottomrule
\end{tabularx}

Which gives this:

enter image description here

There's a small gap highlighted with the red circle, which I don't want to have.

How do I a) make vertical lines in the table such as in the original and b) prevent the small gap due to to the "local" horizontal lines, preferebly using booktabs.

1
  • 4
    booktabs by design makes it hard to have vertical lines. use \cline rather than \cmidrule Commented Jan 8, 2015 at 11:44

2 Answers 2

3

booktabs is not meant to be used with vertical lines. Therefore, I would leave them away and just use some \multicolumns where you need the separator line. This could look like this.

% arara: pdflatex

\documentclass{article}
\usepackage{booktabs}
\usepackage{mhchem}

\begin{document}    
\begin{tabular}{l *{12}c}
    \toprule
    & \ce{NH4^{+}} & \ce{Na+} & \ce{K+} & \ce{Mg^{2+}} & \ce{Zn^{2+}} & \ce{Cu^{2+}} & \ce{Fe^{2+}} & \ce{Fe^{3+}} & \ce{Ca^{2+}} & \ce{Ba^{2+}} & \ce{Pb^{2+}} & \ce{Ag^{+}} \\
    \midrule
    \ce{NO3^{-}} & L & L & L & L & L & L & L & L & L & L & L & L \\ \cline{12-13}
    \ce{Cl-} & L & L & L & L & L & L & L & L & L & L & \multicolumn{1}{|c}{T} & T \\ 
    \ce{Br-} & L & L & L & L & L & L & L & L & L & L & \multicolumn{1}{|c}{T} & T \\ 
    \ce{I-} & L & L & L & L & L & L & L & L & L & L & \multicolumn{1}{|c}{T} & T \\ \cline{10-11}
    \ce{SO4^{2-}} & L & L & L & L & L & L & L & L & \multicolumn{1}{|c}{T} & T & T & T\\
    \bottomrule
\end{tabular}
\end{document}

Note that you will have to reduce the size as it is bigger than the text width. You could rotate it or use some other trick you find on this homepage. There is no use in using tabularx when not using the X columns. As you have seen in your MWE, the lines are ending at \textwidth, but the table content isn't.

enter image description here

1
  • Neat, thanks! I didn't knew you could make x columns just like that within tabular(x).
    – Argo
    Commented Jan 8, 2015 at 16:30
0

As said in other answers, the vertical rules are not at all in the spirit of booktabs. However, if you actually want to use vertical rules (compatible with the rules of booktabs), you can use {NiceTabular} of nicematrix.

Moreover, it's easy with that environment to draw the rules for the separation. The environment {NiceTabular} creates PGF/Tikz nodes under the cells, rows and columns and you can use those nodes to draw whatever rule you want with Tikz in the \CodeAfter.

\documentclass{article}
\usepackage{booktabs}
\usepackage{mhchem}
\usepackage{nicematrix,tikz}

\begin{document}    
\begin{NiceTabular}{l|*{12}c}
    \toprule
    & \ce{NH4^{+}} & \ce{Na+} & \ce{K+} & \ce{Mg^{2+}} & \ce{Zn^{2+}} & \ce{Cu^{2+}} & \ce{Fe^{2+}} & \ce{Fe^{3+}} & \ce{Ca^{2+}} & \ce{Ba^{2+}} & \ce{Pb^{2+}} & \ce{Ag^{+}} \\
    \midrule
    \ce{NO3^{-}} & L & L & L & L & L & L & L & L & L & L & L & L \\ 
    \ce{Cl-} & L & L & L & L & L & L & L & L & L & L & T & T \\ 
    \ce{Br-} & L & L & L & L & L & L & L & L & L & L & T & T \\ 
    \ce{I-} & L & L & L & L & L & L & L & L & L & L & T & T \\ 
    \ce{SO4^{2-}} & L & L & L & L & L & L & L & L & T & T & T & T\\
    \bottomrule
\CodeAfter
    \tikz \draw (3-|last) -| (6-|12) -| (last-|10) ; 
\end{NiceTabular}
\end{document}

You need several compilations (because of the PGF/Tikz nodes).

Output of the above code

You must log in to answer this question.

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