4

I've following table and have obviously a problem... What's wrong with my code?

\documentclass[11pt,a4paper,headings=small]{scrartcl}
\usepackage{siunitx}
\begin{document}
\begin{tabular}{|c
        |S[table-number-alignment = right]
        |S[table-number-alignment = right]
        |S[table-number-alignment = right]
        |S[table-number-alignment = right]
        |S[table-number-alignment = right]
        |}
    \hline
       & {Jahr 1} & {Jahr 2} & {Jahr 3} & {Jahr 5} & {Jahr 5} \\
    \hline
    V1 & 5000     & 5500     & 6050     & 6655     & 7321     \\
    \hline
    V2 & 3,5      & 4        & 4,5      & 5        & 5,5      \\
    \hline
    V3 & 17500    & 22000    & 27225    & 33275    & 40266    \\
    \hline
    V4 & 0,6      & 0,64     & 0,68     & 0,72     & 0,76     \\
    \hline
    V5 & 3000     & 3520     & 4114     & 4792     & 5564     \\
    \hline
    V6 & 2000     & 2000     & 2500     & 2500     & 2500     \\
    \hline
    V7 & 15000    & 15000    & 15000    & 15000    & 15000    \\
    \hline
    V8 & 6250     & 6250     & 6250     & 6250     & 6250     \\
    \hline
\end{tabular}
\end{document}

Output

5

The default values are not sufficient. Since the rows are processed row by row, package siunitx does not know beforehand, how long the numbers are. Therefore, the width of the numbers can be specified with option table-format:

\documentclass[11pt,a4paper,headings=small]{scrartcl}
\usepackage{siunitx}
\begin{document}
\begin{tabular}{|c
        |S[table-number-alignment = right, table-format=5.1]
        |S[table-number-alignment = right, table-format=5.2]
        |S[table-number-alignment = right, table-format=5.2]
        |S[table-number-alignment = right, table-format=5.2]
        |S[table-number-alignment = right, table-format=5.2]
        |}
    \hline
    & {Jahr 1} & {Jahr 2} & {Jahr 3} & {Jahr 5} & {Jahr 5} \\
    \hline
    V1 & 5000 & 5500 & 6050 & 6655 & 7321 \\
    \hline
    V2 & 3,5 & 4 & 4,5 & 5 & 5,5 \\
    \hline
    V3 & 17500 & 22000 & 27225 & 33275 & 40266 \\
    \hline
    V4 & 0,6 & 0,64 & 0,68 & 0,72 & 0,76 \\
    \hline
    V5 & 3000 & 3520 & 4114 & 4792 & 5564 \\
    \hline
    V6 & 2000 & 2000 & 2500 & 2500 & 2500 \\
    \hline
    V7 & 15000 & 15000 & 15000 & 15000 & 15000 \\
    \hline
    V8 & 6250 & 6250 & 6250 & 6250 & 6250 \\
    \hline
\end{tabular}
\end{document}

Result

Also, there are lots of lines. Nicer tables can be done without vertical lines and less horizontal lines, see package booktabs:

\documentclass[11pt,a4paper,headings=small]{scrartcl}
\usepackage{siunitx}
\usepackage{booktabs}
\begin{document}
\begin{tabular}{c
        S[table-format=5.1]
        S[table-format=5.2]
        S[table-format=5.2]
        S[table-format=5.2]
        S[table-format=5.2]
        }
    \toprule
    & {Jahr 1} & {Jahr 2} & {Jahr 3} & {Jahr 5} & {Jahr 5} \\
    \midrule
    V1 & 5000 & 5500 & 6050 & 6655 & 7321 \\
    V2 & 3,5 & 4 & 4,5 & 5 & 5,5 \\
    V3 & 17500 & 22000 & 27225 & 33275 & 40266 \\
    V4 & 0,6 & 0,64 & 0,68 & 0,72 & 0,76 \\
    V5 & 3000 & 3520 & 4114 & 4792 & 5564 \\
    V6 & 2000 & 2000 & 2500 & 2500 & 2500 \\
    V7 & 15000 & 15000 & 15000 & 15000 & 15000 \\
    V8 & 6250 & 6250 & 6250 & 6250 & 6250 \\
    \bottomrule
\end{tabular}
\end{document}

Result

  • Thanks a lot! I thought, the S column could also auto-assign width like the c column, so that was my mistake. – Franz Forstmayr Dec 26 '16 at 23:00
2

You may shorten the code, and add some vertical padding to cells:

\documentclass[11pt, a4paper, headings=small]{scrartcl}
\usepackage{siunitx}

\begin{document}

\sisetup{table-number-alignment = right, table-format =5.2}
\setlength\extrarowheight{2pt}
\begin{tabular}{|c |S[table-format=5.1] |*{4}{S|}}
    \hline
    & {Jahr 1} & {Jahr 2} & {Jahr 3} & {Jahr 5} & {Jahr 5} \\
    \hline
    V1 & 5000 & 5500 & 6050 & 6655 & 7321 \\
    \hline
    V2 & 3,5 & 4 & 4,5 & 5 & 5,5 \\
    \hline
    V3 & 17500 & 22000 & 27225 & 33275 & 40266 \\
    \hline
    V4 & 0,6 & 0,64 & 0,68 & 0,72 & 0,76 \\
    \hline
    V5 & 3000 & 3520 & 4114 & 4792 & 5564 \\
    \hline
    V6 & 2000 & 2000 & 2500 & 2500 & 2500 \\
    \hline
    V7 & 15000 & 15000 & 15000 & 15000 & 15000 \\
    \hline
    V8 & 6250 & 6250 & 6250 & 6250 & 6250 \\
    \hline
\end{tabular}

\end{document} 

enter image description here

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.