4

this is my first question, so please bare with me. I am new to Latex and are using it to create a investigation I have conducted on Snells law. I need assistance with creating a results table. I am unsure on how the results table should be formatted as my teacher has not specified or alluded to. I am also appreciative for any suggestions and/or examples.

I have substituted my data values however the formatting is off and the information and titles needs aligning and reformatting.

This is what I have thus far.

\documentclass{article}
\usepackage{booktabs,multirow,array}
\usepackage[table]{xcolor} % load both xcolor and colortbl
\begin{document}
\begin{table}
\centering
\begin{tabular}{@{} l*{6}{>{$}c<{$}} @{}}
\toprule
Angle of incidence \\($^\circ$)& \multicolumn{5}{c@{}}{Angle of refraction ($^\circ$)} \sin i& \sin r&\\
\cmidrule(l){2-5}
& Trial 1 & Trial 2 & Trial 3 & Average \\
\midrule
\multirow{2}{*}{0} & 0 & 0 & 0 & 0 & 0&0\\
\multirow{2}{*}{15} & 10 & 10& 10& 10.0&0.173648178 &0.258819045\\ 
\multirow{2}{*}{30} & 19.5 &20 & 19.5& 19.7 &0.337095258 & 0.5\\
\multirow{2}{*}{45} & 28& 28& 28 &28.0 &0.469471563 & 0.707106781\\
\multirow{2}{*}{60} & 36&36 & 36&36.0& 0.587785252 & 0.866025404\\ 
\multirow{2}{*}{75} & 40.5& 40& 40& 40.2 &0.645457688 & 0.965925826\\
\bottomrule
\end{tabular}
\caption{Table of results for angle of incidence and angle of refraction between air and an unknown medium}
\end{table}

\end{document}

This is what is produced.

enter image description here

I thank you in advance for your help and suggestions/examples.

5
  • For the numerical columns → siunitx, i.e., section 3.8 of the user manual.
    – cabohah
    Commented Jun 17 at 9:30
  • What is the purpose of those \multirow elements?
    – daleif
    Commented Jun 17 at 9:35
  • @daleif I am not sure this code was copied from the aforementioned answer link in the question, I have limited knowledge of creating tables and I have just started and dont have alot of time to read through the docs.
    – user323614
    Commented Jun 17 at 9:42
  • @cabohah could you please elaborate and potentially show me how I can change the original code I have.
    – user323614
    Commented Jun 17 at 9:43
  • @baccarinx Not the next 2 hours.
    – cabohah
    Commented Jun 17 at 9:51

2 Answers 2

3

There are too many \multicols, and some errors on cells count (on the first rows). Here are two variations:

  • one with tabularx
  • one with tabularray

Feel free to adapt.

\documentclass{article}
\usepackage{booktabs,multirow,array}
\usepackage[table]{xcolor} % load both xcolor and colortbl
\usepackage{tabularx}

\usepackage{tabularray}
\UseTblrLibrary{siunitx}
\UseTblrLibrary{booktabs}
\begin{document}
  \begin{table}
    \centering
    \begin{tabularx}{\linewidth}{@{} X*{6}{>{$}c<{$}} @{}} % you define here a table with 1 left aligned colum + 6 centered columns in math mode
      \toprule
%      Angle of incidence \\ % these two backslashes break to a new line in the table
%      ($^\circ$)& \multicolumn{5}{c@{}}{Angle of refraction ($^\circ$)} \sin i& \sin r&\\ % to many cells in this row
      Angle of incidence ($^\circ$) & \multicolumn{4}{c}{Angle of refraction ($^\circ$)} & \sin i & \sin r \\
      \cmidrule(l){2-5}
      & Trial 1 & Trial 2 & Trial 3 & Average &&\\
      \midrule
      0  & 0 & 0 & 0 & 0 & 0&0\\
      15 & 10 & 10& 10& 10.0&0.173648178 &0.258819045\\ 
      30 & 19.5 &20 & 19.5& 19.7 &0.337095258 & 0.5\\
      45 & 28& 28& 28 &28.0 &0.469471563 & 0.707106781\\
      60 & 36&36 & 36&36.0& 0.587785252 & 0.866025404\\ 
      75 & 40.5& 40& 40& 40.2 &0.645457688 & 0.965925826\\
      \bottomrule
    \end{tabularx}
    \caption{Table of results for angle of incidence and angle of refraction between air and an unknown medium}
  \end{table}
  
  \begin{table}
    \centering
    \begin{tblr}{colspec={@{} X*{4}{S[table-format=2.1]}*{2}{S[table-format=1.9]} @{}}} % you define here a table with 1 left aligned colum + 6 centered columns in math mode
      \toprule
      \SetCell[r=2,c=1]{} Angle of incidence ($^\circ$) & \SetCell[r=1,c=4]{c} {{{Angle of refraction ($^\circ$)}}} &&&& $\sin i$ & $\sin r$ \\
      \cmidrule{2-5}
      & {{{Trial 1}}} & {{{Trial 2}}} & {{{Trial 3}}} & {{{Average}}} &&\\
      \midrule
      0  & 0 & 0 & 0 & 0 & 0&0\\
      15 & 10 & 10& 10& 10.0&0.173648178 &0.258819045\\ 
      30 & 19.5 &20 & 19.5& 19.7 &0.337095258 & 0.5\\
      45 & 28& 28& 28 &28.0 &0.469471563 & 0.707106781\\
      60 & 36&36 & 36&36.0& 0.587785252 & 0.866025404\\ 
      75 & 40.5& 40& 40& 40.2 &0.645457688 & 0.965925826\\
      \bottomrule
    \end{tblr}
    \caption{Table of results for angle of incidence and angle of refraction between air and an unknown medium}
  \end{table}
\end{document}

enter image description here

Edit

Adjusting the colsep and setting the sin centered in title rows:

\begin{table}
    \centering
    \begin{tblr}{colsep = 4pt,colspec={@{} X*{4}{S[table-format=2.1]}*{2}{S[table-format=1.9]} @{}}} % you define here a table with 1 left aligned colum + 6 centered columns in math mode
      \toprule
      \SetCell[r=2,c=1]{} Angle of incidence ($^\circ$) & \SetCell[r=1,c=4]{c} {{{Angle of refraction ($^\circ$)}}} &&&& \SetCell[r=2,c=1]{}$\sin i$ & \SetCell[r=2,c=1]{}$\sin r$ \\
      \cmidrule{2-5}
      & {{{Trial 1}}} & {{{Trial 2}}} & {{{Trial 3}}} & {{{Average}}} &&\\
      \midrule
      0  & 0 & 0 & 0 & 0 & 0&0\\
      15 & 10 & 10& 10& 10.0&0.173648178 &0.258819045\\ 
      30 & 19.5 &20 & 19.5& 19.7 &0.337095258 & 0.5\\
      45 & 28& 28& 28 &28.0 &0.469471563 & 0.707106781\\
      60 & 36&36 & 36&36.0& 0.587785252 & 0.866025404\\ 
      75 & 40.5& 40& 40& 40.2 &0.645457688 & 0.965925826\\
      \bottomrule
    \end{tblr}
    \caption{Table of results for angle of incidence and angle of refraction between air and an unknown medium}
  \end{table}

enter image description here

Edit 2

  \begin{table}
    \centering
    \begin{tblr}{colsep = 4pt,colspec={@{} X*{4}{S[table-format=2.1]}*{2}{c} @{}},column{6}={mode=math},column{7}={mode=math}} % you define here a table with 1 left aligned colum + 6 centered columns in math mode
      \toprule
      \SetCell[r=2,c=1]{} Angle of incidence ($^\circ$) & \SetCell[r=1,c=4]{c} {{{Angle of refraction ($^\circ$)}}} &&&& \SetCell[r=2,c=1]{}\sin i & \SetCell[r=2,c=1]{}\sin r \\
      \cmidrule{2-5}
      & {{{Trial 1}}} & {{{Trial 2}}} & {{{Trial 3}}} & {{{Average}}} &&\\
      \midrule
      0  & 0 & 0 & 0 & 0 & 0&0\\
      15 & 10 & 10& 10& 10.0&0.173648178 &0.258819045\\ 
      30 & 19.5 &20 & 19.5& 19.7 &0.337095258 & 0.5\\
      45 & 28& 28& 28 &28.0 &0.469471563 & 0.707106781\\
      60 & 36&36 & 36&36.0& 0.587785252 & 0.866025404\\ 
      75 & 40.5& 40& 40& 40.2 &0.645457688 & 0.965925826\\
      \bottomrule
    \end{tblr}
    \caption{Table of results for angle of incidence and angle of refraction between air and an unknown medium}
  \end{table}
6
  • the second example is almost perfect and what I need but how could I reformat to center sin i and sin r rather than them being at the \toprule
    – user323614
    Commented Jun 17 at 10:26
  • The data values under angle of incidence, sin i and sin r are not centered like those under the trials and average How can those also be centered under their corresponding headings?
    – user323614
    Commented Jun 17 at 10:45
  • 1
    I use a S-type column in order to format those number aligned on the comma. Have a look at the siunitx documentation, or use a standard c column (see the second edit)
    – NBur
    Commented Jun 17 at 10:52
  • 1
    As you can see in @Mico's answer, S-type columns are pretty useful, and you can take into account its suggestion of rounding the values…
    – NBur
    Commented Jun 17 at 10:55
  • Edit 2 Centers the sin i and sin r however angles 0, 15, 30, 45, 60, 75 under the Angle of incidence heading are not centered.
    – user323614
    Commented Jun 17 at 11:08
5

I recommend that you employ the siunitx package and its S column type for the six data columns, both to align the numbers on their (explicit or implicit) decimal markers and to round the numbers in the final two columns to a reasonable number of digits, say, 4. (Aside: What might be the purpose of showing 9 [!] digits of significance? Do you maybe expect your readers to take in all 9 digits of every number in the final two columns?)

enter image description here

\documentclass{article}
\usepackage{booktabs}
\usepackage{amsmath}
\usepackage{siunitx}
\newcommand\mytab[1]{\smash[b]{%
   \begin{tabular}[t]{@{}l@{}} #1 \end{tabular}}}
   
\begin{document}

\begin{table}[ht]
\centering
\begin{tabular}{@{} 
                l 
                *{4}{S[table-format=2.1]} 
                *{2}{S[table-format=1.4,
                       round-precision=4,
                       round-mode=places,
                       drop-zero-decimal=true]} 
                @{}}
              
\toprule
\mytab{Angle of\\incidence ($^\circ$)} & \multicolumn{4}{c@{}}{Angle of refraction ($^\circ$)}  &
{$\sin i$} & {$\sin r$} \\
\cmidrule(lr){2-5}
& {Trial 1} & {Trial 2} & {Trial 3} & {Average} \\
\midrule
0  & 0    & 0  & 0    & 0    & 0           & 0   \\
15 & 10   & 10 & 10   & 10.0 & 0.173648178 & 0.258819045\\
30 & 19.5 & 20 & 19.5 & 19.7 & 0.337095258 & 0.5 \\
45 & 28   & 28 & 28   & 28.0 & 0.469471563 & 0.707106781\\
60 & 36   & 36 & 36   & 36.0 & 0.587785252 & 0.866025404\\
75 & 40.5 & 40 & 40   & 40.2 & 0.645457688 & 0.965925826\\
\bottomrule
\end{tabular}

\caption{Table of results for angle of incidence ($i$) and angle of refraction ($r$) between air and an unknown medium}

\end{table}

\end{document} 

You must log in to answer this question.