2

I am trying to create a graph in Latex with the given code:

\begin{table}[htbp]
  \centering
  \caption{Table showing the processed data.}
    \scalebox{0.7}{
    \begin{tabular}{|c|c|c|c|}
    \hline
    \multicolumn{1}{l}{Average distance, x/ $\SI{}{cm}$} & \multicolumn{1}{l}{Uncertainty in average distance/ $\pm \SI{}{cm}$} & \multicolumn{1}{l}{Average distance squared/ $\SI{}{cm^2}$} & \multicolumn{1}{l}{Uncertainty in average distance squared / $\pm \SI{}{cm^2}$} \\
    \hline
    18.2  & 0.3   & 332   & 10.9 \\
    24.4  & 0.5   & 597   & 24.4 \\
    28.0  & 0.6   & 786   & 33.6 \\
    35.7  & 0.8   & 1280  & 57.2 \\
    38.6  & 0.5   & 1490  & 38.6 \\
    45.2  & 0.5   & 2050 & 45.2 \\
    49.1  & 1.1   & 2410 & 103.1 \\
    56.5  & 1.2   & 3190 & 130.0 \\
    59.7  & 1.2   & 3560 & 143.2 \\
    61.1  & 3.6   & 3730 & 433.8 \\
    63.3  & 1.0   & 4000 & 120.2 \\
    \hline
    \end{tabular}
    }
  \label{tab:addlabel}%
\end{table}%

But when I compile, the table looks very uneven: enter image description here

What is causing the table to align itself in this way and how can I fix this issue?

Thanks, Aidanaidan12

David Carlisle
  • 757,742

2 Answers2

1

See if you like the following table design:

enter image description here

\documentclass{article}
\usepackage{booktabs, makecell}
\usepackage{siunitx}

\begin{document}
\begin{table}[htbp]
  \centering
  \caption{Table showing the processed data.}
    \begin{tabular}{S[table-format=2.1]
                    S[table-format=1.1]
                    S[table-format=4.0]
                    S[table-format=3.1]}
    \toprule
{\thead[b]{Average\\ distance,\\ x/ $\SI{}{cm}$}}
    & {\thead[b]{Uncertainty in\\ average distance\\  $\pm$ \si{cm}}}
        & {\thead[b]{Average distance\\ squared\\ \si{cm^2}}}
            & {\thead[b]{Uncertainty in average\\ distance squared\\ $\pm$ \si{cm^2}}} \\
    \midrule
    18.2  & 0.3   & 332   & 10.9 \\
    24.4  & 0.5   & 597   & 24.4 \\
    28.0  & 0.6   & 786   & 33.6 \\
    35.7  & 0.8   & 1280  & 57.2 \\
    38.6  & 0.5   & 1490  & 38.6 \\
    45.2  & 0.5   & 2050 & 45.2 \\
    49.1  & 1.1   & 2410 & 103.1 \\
    56.5  & 1.2   & 3190 & 130.0 \\
    59.7  & 1.2   & 3560 & 143.2 \\
    61.1  & 3.6   & 3730 & 433.8 \\
    63.3  & 1.0   & 4000 & 120.2 \\
    \bottomrule
    \end{tabular}
  \label{tab:addlabel}%
\end{table}
\end{document}
Zarko
  • 296,517
1

To allow the table to fit inside the width of the text block, you must allow line breaking in all four header cells. Here's a solution which uses a tabularx environment (with a centered version of the X column type for the headers, to allow automatic line breaking), the c column type for the first two data columns, and the S column type for the final two data columns (to perform alignment on the explicit or implicit decimal markers).

Oh, and do please write \si{cm} instead of `\SI{}{cm}.

enter image description here

\documentclass{article}
\usepackage{siunitx,tabularx,booktabs}
\newcolumntype{C}{>{\centering\arraybackslash}X}
\newcommand\mC[1]{\multicolumn{1}{@{}C@{}}{#1}}

\begin{document} 
\begin{table}[htbp]
\caption{Table showing the processed data.}
\label{tab:addlabel}
    \begin{tabularx}{\textwidth}{@{}
       cc
       S[table-format=4.0]
       S[table-format=3.1]@{}}
    \toprule
    \mC{Average distance, x/ \si{cm}} & 
    \mC{Uncertainty in average distance/ $\pm \si{cm}$} & 
    \mC{Average distance squared/ \si{cm^2}} & 
    \mC{Uncertainty in average distance squared/ $\pm \si{cm^2}$} \\
    \midrule
    18.2  & 0.3   &  332  & 10.9 \\
    24.4  & 0.5   &  597  & 24.4 \\
    28.0  & 0.6   &  786  & 33.6 \\
    35.7  & 0.8   & 1280  & 57.2 \\
    38.6  & 0.5   & 1490  & 38.6 \\
    45.2  & 0.5   & 2050  & 45.2 \\
    49.1  & 1.1   & 2410 & 103.1 \\
    56.5  & 1.2   & 3190 & 130.0 \\
    59.7  & 1.2   & 3560 & 143.2 \\
    61.1  & 3.6   & 3730 & 433.8 \\
    63.3  & 1.0   & 4000 & 120.2 \\
    \bottomrule
    \end{tabularx}
\end{table}
\end{document}
Mico
  • 506,678