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}

Nick B
  • 831
  • 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??? – David Carlisle Jun 14 '22 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 '22 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 '22 at 22:10
  • @NickB Centered how? Horizontally? It should be automatic, but your use of \vtop and \strut prevents it. – Miyase Jun 14 '22 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 – David Carlisle Jun 14 '22 at 22:12
  • is there a better way to line break the row in tabular? I tried searching earlier – Nick B Jun 14 '22 at 22:12
  • Use a p -type column (for example). – Miyase Jun 14 '22 at 22:13
  • nesed tabular or p column, or simply use two rows of the table – David Carlisle Jun 14 '22 at 22:13
  • I just tried a couple of things. If you don't like having the headers on a single line (for which column type c works wonder, no need for TeX primitives), then David Carlisle's suggestion of using two rows looks the best to me. – Miyase Jun 14 '22 at 22:21

2 Answers2

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

L.J.R.
  • 10,932
Nick B
  • 831
  • 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 '22 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 '22 at 05:02
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

Miyase
  • 2,544
  • 2
  • 12
  • 26