0

I am trying to align column 1 row 1 and column 2 row 1 better (independently) (specifically the Time and seconds, x and also the height and feet, y. You can see each is off center a bit. I am unable to figure out how to center them better. Any help would be much appreciated.

\documentclass{article}
\usepackage[utf8]{inputenc}

\title{Tabular Example}
\author{nbennett }
\date{June 2022}

\begin{document}

\maketitle

\section{Table}
\begin{tabular}{|c|c|}
\hline
\vtop{\hbox{\strut Time}\hbox{\strut (seconds), \(x\)}}&\vtop{\hbox{\strut Height}\hbox{\strut (feet), \(y\)}}\\
\hline
0&8 \\ \hline
0.5 & 20 \\ \hline
1   & 24 \\ \hline
1.5 & 20 \\ \hline
2   & 8 \\ \hline
\hline
\end{tabular}

\end{document}
9
  • I am not sure why you say they are off centre? the headings are the widest entry so centering, left, or right aligment are the same. But why the tex primitve code rather than use latex??? Jun 14, 2022 at 22:08
  • Your question isn't clear. What are you trying to align? The c option will automatically center each cell content. Can you post a description of what you want? Also: use of \vtop and \strut... probably not the best idea.
    – Miyase
    Jun 14, 2022 at 22:08
  • I am wondering if the word Time and seconds x can be more centered and also height and feet, y @Miyase
    – Nick B
    Jun 14, 2022 at 22:10
  • @NickB Centered how? Horizontally? It should be automatic, but your use of \vtop and \strut prevents it.
    – Miyase
    Jun 14, 2022 at 22:11
  • You have put them in a primitive \hbox so it will not be centered within the vbox, but the vbox as a whole is centred in the column Jun 14, 2022 at 22:12

2 Answers 2

1

If I understand correctly: you don't want the header on a single line (perhaps because it'd be too wide?) and you want it horizontally centered. Here's something that's very close to your second attempt, but a bit cleaner.

\begin{tblr}{colspec={cc}, vlines, hline{1,3-8}, rowsep=0pt}
Time & Height\\
(seconds), $x$ & (feet), $y$\\
0   &  8 \\
0.5 & 20 \\
1   & 24 \\
1.5 & 20 \\
2   & 8 \\
\end{tblr}

result

1
  • thank you for taking the time to do this.
    – Nick B
    Jun 14, 2022 at 22:56
1

Here is something I came up with. Alignment is better.

\begin{tblr}{colspec={|c|c|c|},hlines}
    {Time \\ (seconds), \(x\)} & {Height\\(feet),\(y\)}           \\
    0.5 & 20 \\
1   & 24 \\ 
1.5 & 20 \\
2   & 8 \\ 
\end{tblr}

Found the code from another post

Table with multiple lines in some cells

enter image description here

2
  • 1
    If all you want is to have the header horizontally centered and on two lines, using two rows as suggested above is both cleaner and simpler. I'll post some code in a minute.
    – Miyase
    Jun 14, 2022 at 22:44
  • In my opinion, this method is better than using two rows. You can replace the argument of tblr with colspec={cc}, hlines, vlines and tabularray will automatically create the correct lines. This is easier and more flexible than @Miyase solution.
    – marv
    Jun 15, 2022 at 5:02

You must log in to answer this question.

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