3

I have a data table and I want to center the data values underneath the heading "Angle of incidence", so rather than the numbers 0, 15, 30, 45, 60, 75 being situated left I want them to be centered like those found underneath the sin i and sin r headings.

This is my code

\documentclass[10pt]{article}
\usepackage[labelfont=bf, skip=5pt, font=small]{caption}
\usepackage{subcaption}
%for tables
\usepackage{booktabs,multirow,array}
\usepackage[table]{xcolor} % load both xcolor and colortbl
\usepackage{tabularx}
\usepackage{tabularray}
\UseTblrLibrary{siunitx}
\UseTblrLibrary{booktabs}
\usepackage{graphicx}
\usepackage{lmodern}
\usepackage{tikz}
\usepackage{sidenotes}
\usepackage{pgfplots}
\usepackage{tocloft}
\usepackage{fancyhdr}
\usepackage{titlesec,titletoc}
\usepackage{pdflscape}
\usepackage{everypage-1x}
\usepackage{amsmath}




\usepackage{wrapfig}
\usepackage{enumitem}
\setlength\evensidemargin{\oddsidemargin}
\usepackage[a4paper, margin=2.56cm]{geometry}

\usepackage{datetime}
\newdateformat{auDateFormat}{\ordinal{DAY} \monthname[\THEMONTH] \THEYEAR}
\auDateFormat
\pgfplotsset{compat=1.18} 
\begin{document}

\section{Results and discussion}

  \begin{table}[h]
    \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 ($i$) and angle of refraction ($r$) between air and an unknown medium}
  \end{table}

\end{document}

It produces this

enter image description here

As can be seen there is alot of horizontal space and the angles underneath angle of incidence heading are not centered.

enter image description here


EDIT (with code borrowed from this answer):

This is how I want it to look; if possible, I want to have the data values in sin i and sin r centered please.

enter image description here

\documentclass{article}
\usepackage{booktabs}
\usepackage{amsmath}
\usepackage{siunitx}
\newcommand\mytab[1]{\smash[b]{%
   \begin{tabular}[t]{@{}c@{}} #1 \end{tabular}}} % use 'c' col. type
   
\begin{document}

\begin{table}[ht]
\centering
\begin{tabular}{@{} 
                S[table-format=2.0] 
                *{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.0   & 10.0 & 10   & 10.0 & 0.173648178 & 0.258819045\\
30 & 19.5 & 20.0 & 19.5 & 19.7 & 0.337095258 & 0.5 \\
45 & 28.0   & 28.0 & 28.0   & 28.0 & 0.469471563 & 0.707106781\\
60 & 36.0   & 36.0 & 36.0   & 36.0 & 0.587785252 & 0.866025404\\
75 & 40.5 & 40.0 & 40.0   & 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} 
5
  • Well, currently these four columns are formatted as S[table-format=2.1] which aligns numbers by decimal dot. Change the format to c, and the numbers will become centered. Commented Jun 17 at 12:47
  • @SergeiGolovan - I think the OP is concerned with the first column, not the next four columns.
    – Mico
    Commented Jun 17 at 12:49
  • @Mico, Oh, you're right, I misread the question Commented Jun 17 at 12:51
  • Are you commited to making the first column so very wide?
    – Mico
    Commented Jun 17 at 12:53
  • 1
    I like your answer that you outlined but the other data values underneath trials, average, sin i, and sin r are no longer centered
    – user323614
    Commented Jun 17 at 12:54

1 Answer 1

3

Building on the code given in this answer to your earlier question, I suggest changing the column type of the first column from l to S[table-format=2.0], changing the column type employed by the \mytab macro from l to c, and encasing the entire \mytab{...} material in curly braces.

enter image description here

\documentclass{article}
\usepackage{booktabs}
\usepackage{amsmath}
\usepackage{siunitx}
\newcommand\mytab[1]{\smash[b]{%
   \begin{tabular}[t]{@{}c@{}} #1 \end{tabular}}} % use 'c' col. type
   
\begin{document}

\begin{table}[ht]
\centering
\begin{tabular}{@{} 
                S[table-format=2.0] 
                *{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} 


Addendum to address one of the OP's follow-up comments: If you prefer the zeros in the first row and final two columns to be centered horizontally rather than be aligned on their implicit decimal markers, just encase them in curly braces; doing so will exempt them from being "treated" by siunitx.

enter image description here

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

\begin{table}[ht]
\centering
\begin{tabular}{@{} 
                S[table-format=2.0] 
                S[table-format=2.1]
                S[table-format=2.0]
                *{2}{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}   \\ % <-- note: "{0}" rather than "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} 


Second addendum, posted to summarize the discussions held in the chat. (They included the topic of how to limit the width of the caption to that of the associated tabular environment.)

enter image description here

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

\usepackage{multirow}
\usepackage{threeparttable}

\begin{document}

\begin{table}[ht]
\centering

\begin{threeparttable}
\begin{tabular}{@{} 
                     S[table-format=2.0] 
                     S[table-format=2.1]
                     S[table-format=2.0]
                *{2}{S[table-format=2.1]} 
                *{2}{S[table-format=1.4,
                       round-precision=4,
                       round-mode=places,
                       %%drop-zero-decimal=true % no longer needed
                       ]} 
                @{}}
              
\toprule
{\mytab{Angle of inci-\\dence $i$ ($^\circ$)}} & \multicolumn{4}{c@{}}{Angle of refraction $r$ ($^\circ$)} &
{\multirow{2.4}{*}{$\sin i$}} & 
{\multirow{2.4}{*}{$\sin r$}} \\
\cmidrule(lr){2-5}
& {Trial 1} & {Trial 2} & {Trial 3} & {Average} \\
\midrule
0  & 0    & 0  & 0    & 0    & {0} & {0} \\ % <-- note: "{0}" rather than "0"
15 & 10   & 10 & 10   & 10.0 & 0.173648178 & 0.258819045\\
30 & 19.5 & 20 & 19.5 & 19.7 & 0.337095258 & {0.5\phantom{000}} \\
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{threeparttable}

\end{table}

\end{document} 
11
  • That works great however it has reverted to cause another problem I had already received an solution for. The data values underneath the trial, average, sin i, and sin r headings are no longer centered as seen in the original code.
    – user323614
    Commented Jun 17 at 12:52
  • @baccarinx - I disagree. To my eye, the headers of the data columns in question are centered horizontally above the respective numbers. It's the numbers that create a "ragged" look; but that's by design, as not all numbers possess the same number of decimal digits. To verify this claim, just backfill zeros where they are currently suppressed.
    – Mico
    Commented Jun 17 at 12:55
  • Ok I agree with you however the values underneath sin i and sin r are clearly not centered as the 0 and 0 in first trial are not centered in the middle
    – user323614
    Commented Jun 17 at 12:56
  • 1
    If it can be avoided, I would like 0 by itself without being 0.0000
    – user323614
    Commented Jun 17 at 13:03
  • 1
    Let us continue this discussion in chat.
    – user323614
    Commented Jun 17 at 13:31

You must log in to answer this question.