8

I am trying to horizontally align text that has been rotated in a table. I want the rotated "Lorem Ipsum" and the rotated "Lorem Ipsum & Lorem Ipsum" text to be horizontally centered in the cell. When I adjust the width of each column manually, the alignment is no longer centered. I need to modify the width of each column so that my table will fit in the width of a single column.

I tried using the \multirow command from How can I align rotated text in a table at the bottom? but I was only able to modify the vertical alignment.

Here is what my table looks like:

Table

Here is the code (apologies if I included some unnecessary packages):

\documentclass{article}
\usepackage{array}
\usepackage{tabularx}
\usepackage{rotating}
\usepackage{lipsum}
\usepackage{multirow}
\begin{document}

\newcommand\RotText[1]{\fontsize{9}{9}\selectfont \rotatebox[origin=c]{90}{\parbox{2.6cm}{\centering#1}}}
\newcolumntype{G}{>{\centering\arraybackslash}m{.0625cm}}
\newcolumntype{U}{>{\centering\arraybackslash}m{.375cm}}

{\centering
\begin{center}\begin{table}[ht]\caption{Lorem Ipsum Table}
\footnotesize
\centering
\begin{tabular}{|c|G|U|U|U|U|G|G|G|U|}
\hline
 & \multicolumn{9}{c|}{Lorem Ipsum} \\ 
\cline{2-10}

Instruction & \RotText{Lorem Ipsum} & \RotText{Lorem Ipsum \& Lorem Ipsum} & \RotText{Lorem Ipsum \& Lorem Ipsum} & \RotText{Lorem Ipsum \& Lorem Ipsum} & 
\RotText{Lorem Ipsum \& Lorem Ipsum} & \RotText{Lorem Ipsum} & \RotText{Lorem Ipsum} & \RotText{Lorem Ipsum} & \RotText{Lorem Ipsum \& Lorem Ipsum} \\
\hline
Lorem Ipsum     & 
& & X   & & & & & & \\

\hline
\end{tabular}
\end{table}
\end{center}
}

\lipsum

\end{document}
Veridian
  • 743
  • 2
  • 8
  • 23

3 Answers3

5

Your box was far wider than the specified width of the column, so centreing could not work. Also don't put the table inside a center environment it will float away leaving spurious vertical space from the center display with nothing in it.

enter image description here

\documentclass{article}
\usepackage{array}
\usepackage{tabularx}
\usepackage{rotating}
\usepackage{lipsum}
\usepackage{multirow}
\begin{document}

\newcommand\RotText[1]{\rotatebox[origin=c]{90}{\parbox{2.6cm}{\centering#1}}}
\newcolumntype{G}{>{\centering\arraybackslash}m{.6cm}}
\newcolumntype{U}{>{\centering\arraybackslash}m{.6cm}}

\setlength\extrarowheight{3pt}
\begin{table}[ht]\caption{Lorem Ipsum Table}
\footnotesize
\centering
\begin{tabular}{|c|G|U|U|U|U|G|G|G|U|}
\hline
 & \multicolumn{9}{c|}{Lorem Ipsum} \\ 
\cline{2-10}

Instruction & \RotText{Lorem Ipsum} & \RotText{Lorem Ipsum \& Lorem Ipsum} & \RotText{Lorem Ipsum \& Lorem Ipsum} & \RotText{Lorem Ipsum \& Lorem Ipsum} & 
\RotText{Lorem Ipsum \& Lorem Ipsum} & \RotText{Lorem Ipsum} & \RotText{Lorem Ipsum} & \RotText{Lorem Ipsum} & \RotText{Lorem Ipsum \& Lorem Ipsum} \\
\hline
Lorem Ipsum     & 
& & X   & & & & & & \\

\hline
\end{tabular}
\end{table}


\lipsum

\end{document}
David Carlisle
  • 757,742
  • Thank you for your help, however this doesn't fix my problem. The columns are too wide now. You modified the newcolumntype and when I go back to adjust it, the same problem occurs. – Veridian Mar 19 '14 at 16:21
  • @sphere well make them smaller (but not as small as you had it which doesn't leave room for visible text at all .0625cm is really very narrow. Latex warns on the command line for every overfull box so just reduce the font size till it fits. – David Carlisle Mar 19 '14 at 16:39
3

REVISED SOLUTION

One of the problems with your MWE and my earlier tweak thereof was that it did not account for the natural space allocated, by default, between columns, defined by the length \tabcolsep. If narrow is desired, the first thing to do is turn that off, with \setlength\tabcolsep{0pt}. Then, there are no \vspace tweaks required, and the problem becomes determining the column width that satisfies your requirement.

Here, I strove to make the columns as narrow as possible, again, with no tweaking. Note, I saved a copy of \tabcolsep into \svtabcolsep, if I need to reinstate it later.

\documentclass{article}
\usepackage{array}
\usepackage{tabularx}
\usepackage{rotating}
\usepackage{lipsum}
\usepackage{multirow}
\begin{document}

\let\svtabcolsep\tabcolsep
\setlength\tabcolsep{0pt}
\newcommand\RotText[1]{\fontsize{9}{9}\selectfont
  \rotatebox[origin=c]{90}{\parbox{2.6cm}{%
\centering#1}}}
\newcolumntype{G}{>{\centering\arraybackslash}m{.35cm}}
\newcolumntype{U}{>{\centering\arraybackslash}m{.62cm}}

\begin{table}[ht]\caption{Lorem Ipsum Table}
\footnotesize
\centering
\begin{tabular}{|c|G|U|U|U|U|G|G|G|U|}
\hline
 & \multicolumn{9}{c|}{Lorem Ipsum} \\ 
\cline{2-10}

Instruction & \RotText{Lorem Ipsum} & \RotText{Lorem Ipsum \& Lorem Ipsum} & \RotText{Lorem Ipsum \& Lorem Ipsum} & \RotText{Lorem Ipsum \& Lorem Ipsum} & 
\RotText{Lorem Ipsum \& Lorem Ipsum} & \RotText{Lorem Ipsum} & \RotText{Lorem Ipsum} & \RotText{Lorem Ipsum} & \RotText{Lorem Ipsum \& Lorem Ipsum} \\
\hline
Lorem Ipsum     & 
& & X   & & & & & & \\

\hline
\end{tabular}
\end{table}

\lipsum

\end{document}

enter image description here

Note you could even dispense with the G and U column types, making them instead c, and using a small, finite value of \tabcolsep to achieve your goal.

\let\svtabcolsep\tabcolsep
\setlength\tabcolsep{.3pt}
\newcolumntype{G}{c}
\newcolumntype{U}{c}
  • Not to be picky, but the rotated "Lorem Ipsum"s still aren't centered. – Veridian Mar 19 '14 at 16:35
  • .0625cm wide table columns are distinctly odd, but you are suggesting changing them to be 0cm wide? – David Carlisle Mar 19 '14 at 16:40
  • Thanks looks good. I'm not a huge fan of "hacks" with magic numbers. I wish latex packages would have more automatic handling of centering. – Veridian Mar 19 '14 at 16:44
  • @DavidCarlisle I am not a tabular whiz, so in this case, rather than redoing it from scratch, I started with the OP's code, and "tweaked." Tweaking is under-rated. I was doing it long before that Miley whats-her-name girl. – Steven B. Segletes Mar 19 '14 at 16:45
  • @sphere latex will centre automatically with no problems but not if you ask it to centre visible text in a column that is only half a millimetre wide, it can't centre it so it sticks it out one side/ – David Carlisle Mar 19 '14 at 16:55
  • @sphere I may have produced the result you desired, but if David Carlisle gives you warning about the approach, it would be wise to pay heed. He authored various tabular packages (and maybe tabular itself ;^). – Steven B. Segletes Mar 19 '14 at 17:01
  • @DavidCarlisle Revised to avoid tweaking. – Steven B. Segletes Mar 19 '14 at 17:13
  • @sphere I revised my answer to avoid so-called "hacks." – Steven B. Segletes Mar 19 '14 at 17:16
  • @StevenB.Segletes, How would I make your final width command to only this table and not other tables within the same document? – Veridian Mar 19 '14 at 18:24
  • @sphere After the table is done, \let\tabcolsep\svtabcolsep. Alternately, the original \let\svtabcolsep\tabcolsep could have been done just inside the table, and its effect would evaporate upon leaving the table. – Steven B. Segletes Mar 19 '14 at 18:38
  • Also, what does MWE mean? – Veridian Mar 19 '14 at 18:38
  • @sphere MWE = Minimum Working Example, a short piece of code, starting with \documentclass and ending with \end{document}, that demonstrates the essence of the issue. – Steven B. Segletes Mar 19 '14 at 18:40
  • Is there any specific reason for using both \centering and \begin{center}? If there is, what does it achieve which only either of them doesn't? – Damitr Mar 26 '20 at 07:47
  • @Damitr I had done it so as to make the minimum number of changes to the OP's original code. However, I see the confusion it can cause, so I haved edited the answer to include only a single instance. – Steven B. Segletes Mar 26 '20 at 09:42
1

If you really want to pack them in tight...

table

\documentclass{article}
\usepackage{array}
\usepackage{tabularx}
\usepackage{rotating}
\usepackage{lipsum}
\usepackage{multirow}
\begin{document}

\newcommand\RotText[1]{\fontsize{9}{9}\selectfont \rotatebox[origin=c]{90}{\parbox{2.6cm}{\centering#1}}}
\newcolumntype{C}{@{\hspace{2pt}}c@{\hspace{1pt}}}

\begin{table}[ht]\caption{Lorem Ipsum Table}
\footnotesize
\centering
\begin{tabular}{|c|C|C|C|C|C|C|C|C|C|}
\hline
 & \multicolumn{9}{c|}{Lorem Ipsum} \\ 
\cline{2-10}

Instruction & \RotText{Lorem Ipsum} & \RotText{Lorem Ipsum \& Lorem Ipsum} & \RotText{Lorem Ipsum \& Lorem Ipsum} & \RotText{Lorem Ipsum \& Lorem Ipsum} & 
\RotText{Lorem Ipsum \& Lorem Ipsum} & \RotText{Lorem Ipsum} & \RotText{Lorem Ipsum} & \RotText{Lorem Ipsum} & \RotText{Lorem Ipsum \& Lorem Ipsum} \\
\hline
Lorem Ipsum     & 
& & X   & & & & & & \\
\hline
\end{tabular}
\end{table}


\lipsum

\end{document}
John Kormylo
  • 79,712
  • 3
  • 50
  • 120