TransWikia.com

Long table header vertical alignment

TeX - LaTeX Asked on December 20, 2021

Consider the example, with two long headings to a table (one is longer):

documentclass{article}
usepackage{graphicx}
usepackage{array}
newcolumntype{C}[1]{>{centeringletnewline\arraybackslashhspace{0pt}}m{#1}}

begin{document}
    begin{table}
        begin{tabular}{|C{2em}|C{2em}|}hline
            rotatebox{90}{Long long heading 1~} & rotatebox{90}{Long long longer longer heading 2~}\ hline %%%%%%%%%%% `~` is inserted at end of headings to keep the margin away
            Text 1 & Text 2\ hline
        end{tabular}
    end{table}
end{document}

Note that both the headings are centrally aligned in the y-axis.
What I want is, the table headings are aligned at the bottom in the y-axis. In other words, the highlighted space would not be there.

4 Answers

In {NiceTabular} of nicematrix, you have a built-in command rotate to do that. However, you have to rebuild c columns with multicolumn since you want to use your C type columns elsewhere.

documentclass{article}
usepackage{nicematrix}

newcolumntype{C}[1]{>{centeringletnewline\arraybackslashhspace{0pt}}m{#1}}

NiceMatrixOptions{cell-space-top-limit=2pt}

begin{document}
begin{table}
begin{NiceTabular}{C{2em}C{2em}}[hvlines]
multicolumn{1}{c}{rotate Long long heading 1} 
& multicolumn{1}{c}{rotate Long long longer longer heading 2}\ 
Text 1 & Text 2\ 
end{NiceTabular}
end{table}
end{document}

Ouput of above code

Answered by F. Pantigny on December 20, 2021

Two cases with use of the rotating and makecell packages:

documentclass[12pt]{article}
usepackage{rotating}
usepackage{makecell}
renewcommandtheadfont{normalsize}

begin{document}
    begin{table}[ht]
    centering
    settowidthrotheadsize{Long Long longer longer heading 2}
begin{tabular}{|c|c|}
    hline
rothead{Long Long heading} 
                    & rothead{Long Long longer longer heading 2} \ 
    hline
makecell{Text\ 1} & makecell{Text\ 2}   \ 
    hline
end{tabular}
    end{table}
or table design which I more prefer:
    begin{table}[ht]
    centering
    settowidthrotheadsize{Long Long heading}
begin{tabular}{|c|c|}
    hline
rothead{Long Long heading}
                    & rothead{Long Long longer longer heading 2} \
    hline
makecell{Text\ 1} & makecell{Text\ 2}   \
    hline
end{tabular}
    end{table}
end{document} 

enter image description here

Answered by Zarko on December 20, 2021

A solution as I suggested in a comment, and a simpler one, based on eqparbox:

  documentclass{article}
usepackage{graphicx}
usepackage{array}
newcolumntype{C}[1]{>{centeringletnewline\arraybackslashhspace{0pt}}m{#1}}
usepackage{eqparbox}

begin{document}

    begin{table}
centering
        begin{tabular}{|C{2em}|C{2em}|}hline
            multicolumn{1}{|p{2em}}{centeringrotatebox[origin = l]{90}{Long long heading 1}~} &multicolumn{1}{|p{2em}|}{centering rotatebox[origin = l]{90}{Long long longer longer heading 2~}}\ hline %%%%%%%%%%% `~` is inserted at end of headings to keep the margin away
            Text 1 & Text 2\ hline
        end{tabular}
qquad
        begin{tabular}{|C{2em}|C{2em}|}hline
            rotatebox{90}{eqmakebox[H][l]{Long long heading 1}~} & rotatebox{90}{eqmakebox[H][l]{Long long longer longer heading 2}~}\ hline %%%%%%%%%%% `~` is inserted at end of headings to keep the margin away
            Text 1 & Text 2\ hline
        end{tabular}
    end{table}

end{document} 

enter image description here

Answered by Bernard on December 20, 2021

My proposal....

documentclass[12pt]{article}
usepackage{amsmath}

usepackage{graphicx}
begin{document}
begin{table}[h]
centering 
begin{tabular}{|c|c|}
hline
rotatebox{90}{Long Long heading~} & rotatebox{90}{Long Long longer longer heading 2~} \ hline
multicolumn{1}{|c|}{begin{tabular}[c]{@{}c@{}}Text\ 1end{tabular}} & begin{tabular}[c]{@{}c@{}}Text\ 2end{tabular} \ hline
end{tabular}
end{table}
end{document} 

enter image description here

Answered by Sebastiano on December 20, 2021

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP