6

I have a tabular that looks more or less like this:

\documentclass{article}
\usepackage{array,booktabs}
\begin{document}
\begin{table}
\begin{tabular}{lrlrlrll}
    \toprule
    Examples & \multicolumn{2}{l}{value1} & \multicolumn{2}{l}{value2} & \multicolumn{3}{l}{value3 (+smth)} \\
    example1 &   1 & -1                   &  12 & +?                   &  12 & +? & (+123)                  \\
    example2 &  12 & +5                   & 123 & +?                   & 123 & +? & (+12)                   \\
    example3 & 123 & -10                  &   1 & +?                   &   1 & +? & (+1234)                 \\
    example4 &  23 & +123                 & 234 & +?                   & 234 & +? & (+23)                   \\
    example5 & 234 & +4                   &  23 & +?                   &  23 & +? & (+234)                  \\
    \bottomrule
\end{tabular}
\end{table}
\end{document}

And it is rendered like this:

Examples   value1      value2    value3 (+smth)
example1     1  -1      12  +?    12  +?  (+123)
example2    12  +5     123  +?   123  +?  (+12)
example3   123  -10      1  +?     1  +?  (+1234)
example4    23  +123   234  +?   234  +?  (+23)
example5   234  +4      23  +?    23  +?  (+234)

This is close to what I want but there are those extra spaces before + and - signs which are both confusing and make it difficult to fit to the page. Also it does not align properly since + and - do not have the same width (not visible in the shown monospace output). Is there a way to get rid of those spaces to make it look like following?

Examples   value1    value2   value3 (+smth)
example1     1-1      12+?     12+?  (+123)
example2    12+5     123+?    123+?  (+12)
example3   123-10      1+?      1+?  (+1234)
example4    23+123   234+?    234+?  (+23)
example5   234+4      23+?     23+?  (+234)

I have looked at examples for aligning with @{} but since there are two possible align characters (+ and -) I couldn't make it work. There is also dcolumn package but I think it also works only with a single character.

3
  • 1
    use r@{}l for each pair also you need the entries in math mode so you get a minus sign not a hyphen Commented Mar 9, 2018 at 13:10
  • 1
    your example produces ! Undefined control sequence and ! LaTeX Error: \begin{table} on input line 14 ended by \end{document} Commented Mar 9, 2018 at 13:16
  • @DavidCarlisle Ahh, sorry, thanks for fixing it.
    – none
    Commented Mar 9, 2018 at 13:19

2 Answers 2

8

Removing the intercolumn space and using math mode

enter image description here

\documentclass{article}
\usepackage{array,booktabs}
\begin{document}
\begin{table}
\begin{tabular}{l*{3}{>{$}r<{$}@{}>{$}l<{$}}>{$}l<{$}}
    \toprule
    Examples & \multicolumn{2}{l}{value1} & \multicolumn{2}{l}{value2} & \multicolumn{3}{l}{value3 (+smth)} \\
    example1 &   1 & -1                   &  12 & +?                   &  12 & +? & (+123)                  \\
    example2 &  12 & +5                   & 123 & +?                   & 123 & +? & (+12)                   \\
    example3 & 123 & -10                  &   1 & +?                   &   1 & +? & (+1234)                 \\
    example4 &  23 & +123                 & 234 & +?                   & 234 & +? & (+23)                   \\
    example5 & 234 & +4                   &  23 & +?                   &  23 & +? & (+234)                  \\
    \bottomrule
\end{tabular}
\end{table}
\end{document}
4
  • I can't read your tabular line but your suggestion in the comment for adding @{} also seems to work fine. I also used math mode for + and - and they aligned properly. Thanks a lot.
    – none
    Commented Mar 9, 2018 at 13:24
  • @none what do you mean by can not read here? >{$}l<{$} is an l column with $ inserted so the entry is in math mode, which is what you want here. Commented Mar 9, 2018 at 13:27
  • What I meant is that it is the first time I saw such a syntax and I don't know how it works. I now see more or less how it works now. *{3}{...} means three times and the rest is for math mode as you said.
    – none
    Commented Mar 9, 2018 at 13:32
  • *{3} is built in syntax in latex, the > syntax comes from the array package (which is part of the core latex distribution) see texdoc array Commented Mar 9, 2018 at 13:39
5

You have three composite columns with the following pattern: number ± number, this can be set up as @{}>{${}}c<{{}$}@{}. A new column type can be handy for this, see below. I would also combine the last column with the previous one to be similar to their header.

\documentclass{article}
\usepackage{array,booktabs}
\begin{document}

\begin{table}
\newcolumntype{C}{ @{}>{${}}c<{{}$}@{} }
\begin{tabular}{l *3{rCl} }
    \toprule
    Examples & \multicolumn{3}{c}{value1} & \multicolumn{3}{c}{value2} & \multicolumn{4}{c}{value3 (+smth)} \\
    example1 & 1   & - & 1                & 12  & + & ?                & 12  & + & ? (+123)               \\
    example2 & 12  & + & 5                & 123 & + & ?                & 123 & + & ? (+12)                \\
    example3 & 123 & - & 10               & 1   & + & ?                & 1   & + & ? (+1234)              \\
    example4 & 23  & + & 123              & 234 & + & ?                & 234 & + & ? (+23)                \\
    example5 & 234 & + & 4                & 23  & + & ?                & 23  & + & ? (+234)               \\ \bottomrule
\end{tabular}
\end{table}

\end{document}

enter image description here

You must log in to answer this question.

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