0

I want the text in each cell to be centered horizontally. But it isn't even though the tabular settings are 'c'. Note I am not trying to use multirow - this is a single row with two lines of text in each cell. Maybe \parbox is not the right way.

\documentclass{article}
\begin{document}
\begin{tabular}{ |c|c| }
\parbox[c]{1.4cm}{foo\\longer} &
\parbox[c]{1.4cm}{bar\\center} \\
\end{tabular}
\end{document}
1
  • 1
    The simplest, in my opinion, would be to load the makecell package and use the eponymous command.
    – Bernard
    Feb 2, 2022 at 22:39

2 Answers 2

2

two more solutions:

\documentclass{article}
\usepackage{makecell}
\usepackage{tabularray}

\begin{document}
with \verb+makecell+:

\medskip
    \begin{tabular}{ |wc{14mm}|wc{14mm}| }
\makecell{foo\\ longer} 
    & \makecell{bar\\ center}
    \end{tabular}

\bigskip
with \verb+tabularray+:

\medskip
    \begin{tblr}{ |Q[c,14mm]|Q[c,14mm]| }
{foo\\ longer}
    & {bar\\ center}
    \end{tblr}   
\end{document}

enter image description here

Edit: Using tabularray and column specification as are in above example, you not need to manually split text in cells:

\documentclass{article}
\usepackage{tabularray}

\begin{document}
    \begin{tblr}{ |Q[c,14mm]|Q[c,14mm]| }
foo longer
    &  bar center
    \end{tblr}
\end{document}

Result is the same as before.

Addendum:

In the case, that table cells can have natural width, as suggested @L.J.R. in his comment below (thank you very much), than you can omit their width prescription:

\documentclass{article}
\usepackage{makecell} 
\usepackage{tabularray}

\begin{document}
with \verb+makecell+:

\medskip
    \begin{tabular}{ |c|c| }
\makecell{foo\\ longer} 
    & \makecell{bar\\ center}
    \end{tabular}

\bigskip
with \verb+tabularray+:

\medskip
    \begin{tblr}{ |c|c| }
{foo\\ longer}
    & {bar\\ center}
    \end{tblr}   
\end{document}

enter image description here

2
  • Remark: with tabularray, you can even remove the 14mm to use natural widths.
    – L.J.R.
    Feb 2, 2022 at 23:57
  • @L.J.R., you are right, but I keep this width since OP nested \parbox width such width. I will add to answer addendum with with suggestion ASAP
    – Zarko
    Feb 3, 2022 at 0:02
1

Without any additional packages use a nested tabular instead of the parbox.

a

\documentclass{article}
\begin{document}
    \begin{tabular}{ |c|c| }
        \begin{tabular}{c}foo\\ longer\end{tabular} &
        \begin{tabular}{c}bar\\ center\end{tabular}
    \end{tabular}
\end{document}

You must log in to answer this question.

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