3

I have the following code, which creates a table by means of the tabular environment. I want the height of the first and second rows (Category and Subcat.1, Subcat.2) to be the same as the width of the first and second columns (the rotated ones). How can I do that? I've tried with arraystretch, but results seem approximated.

EDIT TO CLARIFY THE QUESTION: What I do want is the height between the line below Category 1 and the line below both Subcat. 1 and Subcat. 2 (the blue arrow) to be the same as the width between the vertical line on the right side of Category 2 and the line on the right side of both Subcat. 1 and Subcat. 2 (the red arrow). I mean: I am happy with the width. Now I want to make the height just the same, that is, make the blue arrow as high as wide is the red arrow.

\documentclass{article}
\usepackage{graphicx}
\begin{document}
    \begin{tabular}{ccc|c|}
        & & \multicolumn{2}{ c }{Category 1}                                                    \\ \cline{3-4}
        & & \multicolumn{1}{ c }{Subcat. 1} & \multicolumn{1}{c}{Subcat. 2}                     \\ \cline{3-4}
        &   \multicolumn{1}{|c|}{\rotatebox[origin=c]{90}{Subcat. 2}} & AAAAAAAAAA & BBBBBBBBBB \\ \cline{3-4}
        \raisebox{1cm}[0pt][0pt]{\rotatebox[origin=c]{90}{Category 2}} &
            \multicolumn{1}{|c|}{\rotatebox[origin=c]{90}{Subcat. 1}} & CCCCCCCCCC & DDDDDDDDDD \\ \cline{3-4}
    \end{tabular}
\end{document}

enter image description here

3
  • '\vphantom{\rotatebox{90}{AAAAAAAAAA}}' in the box is a quick and dirty workaround. I hope there are better answers.
    – Huang_d
    Jun 15, 2017 at 9:02
  • I think I don't understand your comment, @Huang_d
    – Cromack
    Jun 15, 2017 at 9:42
  • Meaning that within the box you add a height equivalent to the length of your text. This is done by using rotatebox{90}{Your text} inside of \vphantom. The latter gives you a length equivalent to its contents (without printing them) and no horizontal width. It is not a good answer because it requires you to write your text twice, once as its printed, once invisible as the height.
    – Huang_d
    Jun 15, 2017 at 9:46

3 Answers 3

0

I added a few @{}, a few \Vrules of baselineskip height, and put a few things into \makeboxes of the same width as the height of \Vrule.

\documentclass{article}
\usepackage{graphicx}
\def\Vrule{\rule[-\dp\strutbox]{0pt}{\normalbaselineskip}}
%\def\Vrule{\rule[-\dp\strutbox]{.1pt}{\normalbaselineskip}}% USE THIS TO VERIFY
\begin{document}
    \begin{tabular}{ccc|c|}
        & & \multicolumn{2}{ c }{Category 1}\\ 
        \cline{3-4}
        & & \multicolumn{1}{ c }{\Vrule Subcat. 1} & \multicolumn{1}{c}{Subcat. 2}\\
 \cline{3-4}
        &   \multicolumn{1}{|@{}c@{}|}{\makebox[\normalbaselineskip]{%
              \rotatebox[origin=c]{90}{\Vrule Subcat. 2}}} 
                & AAAAAAAAAA & BBBBBBBBBB \\ \cline{3-4}
        \raisebox{1cm}[0pt][0pt]{\rotatebox[origin=c]{90}{Category 2}} &
            \multicolumn{1}{|@{}c@{}|}{\makebox[\normalbaselineskip]{%
              \rotatebox[origin=c]{90}{\Vrule Subcat. 1}}} 
                & CCCCCCCCCC & DDDDDDDDDD \\ \cline{3-4}
    \end{tabular}
\end{document}

enter image description here

To verify it does what I says it does, uncomment the second \Vrule definition, which just makes it visible. You will see that it exactly spans the two desired dimensions.

enter image description here

The OP asks to make the answer height adjustable, and that is no problem. Change the dimensions of \Vrule, and make the \makebox width the same dimension. EDITED to do the same for the Category 1/2 entries.

\documentclass{article}
\usepackage{graphicx}
\def\Vrule{\rule[-2.5\dp\strutbox]{0pt}{2\normalbaselineskip}}
%\def\Vrule{\rule[-2.5\dp\strutbox]{.1pt}{2\normalbaselineskip}}% USE THIS TO VERIFY
\begin{document}
    \begin{tabular}{@{}c@{}cc|c|}
        & & \multicolumn{2}{ c }{\Vrule Category 1}\\ 
        \cline{3-4}
        & & \multicolumn{1}{ c }{\Vrule Subcat. 1} & \multicolumn{1}{c}{Subcat. 2}\\
 \cline{3-4}
        &   \multicolumn{1}{|@{}c@{}|}{\makebox[2\normalbaselineskip]{%
              \rotatebox[origin=c]{90}{\Vrule Subcat. 2}}} 
                & AAAAAAAAAA & BBBBBBBBBB \\ \cline{3-4}
        \raisebox{1cm}[0pt][0pt]{\rotatebox[origin=c]{90}{%
          \makebox[2\baselineskip]{\Vrule Category 2}}} &
            \multicolumn{1}{|@{}c@{}|}{\makebox[2\normalbaselineskip]{%
              \rotatebox[origin=c]{90}{\Vrule Subcat. 1}}} 
                & CCCCCCCCCC & DDDDDDDDDD \\ \cline{3-4}
    \end{tabular}
\end{document}

enter image description here

One last point...if the \Vrule can always be guaranteed to have a larger footprint than \strut, then one can eliminate the \makeboxes altogether, simplifying the code:

\documentclass{article}
\usepackage{graphicx}
\def\Vrule{\rule[-2.5\dp\strutbox]{0pt}{2\normalbaselineskip}}
%\def\Vrule{\rule[-2.5\dp\strutbox]{.1pt}{2\normalbaselineskip}}% USE THIS TO VERIFY
\begin{document}
    \begin{tabular}{@{}c@{}cc|c|}
        & & \multicolumn{2}{ c }{\Vrule Category 1}\\ 
        \cline{3-4}
        & & \multicolumn{1}{ c }{\Vrule Subcat. 1} & \multicolumn{1}{c}{Subcat. 2}\\
 \cline{3-4}
        &   \multicolumn{1}{|@{}c@{}|}{%
              \rotatebox[origin=c]{90}{\Vrule Subcat. 2}}
                & AAAAAAAAAA & BBBBBBBBBB \\ \cline{3-4}
        \raisebox{1cm}[0pt][0pt]{\rotatebox[origin=c]{90}{%
          \Vrule Category 2}} &
            \multicolumn{1}{|@{}c@{}|}{%
              \rotatebox[origin=c]{90}{\Vrule Subcat. 1}} 
                & CCCCCCCCCC & DDDDDDDDDD \\ \cline{3-4}
    \end{tabular}
\end{document}
6
  • Thank you, Steven. It is quite a comprehensive answer :) However, what I do want to change is the height (see the blue arrow in my question), not the width (the red arrow), which is what I think you change, don't you? Would it be difficult to adapt it to do so?
    – Cromack
    Jun 15, 2017 at 10:42
  • @Cromack No problem...done. See edit. Jun 15, 2017 at 10:47
  • Thank you, that was what I was looking for. Now, I see that I would like to make the space between Category 1 and the line below the same as the width between Category 2 and the vertical line, but I am afraid I didn't ask for that in the question... Would it require a similar approach? I am accepting your answer right now.
    – Cromack
    Jun 15, 2017 at 10:52
  • @Cromack Thank you. I have edited to add that feature, as well. I just had to add a \Vrule to Categories 1/2, place Category 2 in a \makebox, and add some @{} to column 1 specification. Jun 15, 2017 at 10:58
  • @Cromack Note my final comment on the connection between \Vrule and the need or not for the \makebox. Jun 15, 2017 at 11:03
0

Add some padding at the top and bottom of rows with makecell:

\documentclass{article}
\usepackage{graphicx,makecell}
\setcellgapes{3pt}
\begin{document}

\makegapedcells
    \begin{tabular}{*{3}{c}|c|}
        & & \multicolumn{2}{ c }{Category 1} \\ \cline{3-4}
        & & \multicolumn{1}{ c }{Subcat. 1} & \multicolumn{1}{c}{Subcat. 2} \\ \cline{3-4}
        & \multicolumn{1}{|c|}{\rotatebox[origin=c]{90}{Subcat. 2}} & AAAAAAAAAA & BBBBBBBBBB \\ \cline{3-4}
        \raisebox{1cm}[0pt][0pt]{\rotatebox[origin=c]{90}{Category 2}} &
            \multicolumn{1}{|c|}{\rotatebox[origin=c]{90}{Subcat. 1}} & CCCCCCCCCC & DDDDDDDDDD \\ \cline{3-4}
    \end{tabular}

\end{document} 

enter image description here

6
  • I see that you type 3pt, and I can try with different lengths, but what's the exact length?
    – Cromack
    Jun 15, 2017 at 9:22
  • Yes, you can try different lengths. Do you mean what's the value of 3 pt?
    – Bernard
    Jun 15, 2017 at 9:32
  • What I mean is whether there is a way to know the exact length without trying different lengths?
    – Cromack
    Jun 15, 2017 at 9:35
  • Yes and no: as you can notice, the rotated text does not overflow in the above row, all you have to do is adding a small length so the two rotated texts have a visible space between them. I'd say something like 2 to 4 pt are correct.
    – Bernard
    Jun 15, 2017 at 9:53
  • Oh, I think I must have asked the question wrongly... What I do want is the height between the line below Category 1 and the lines below both Subcat. 1 and Subcat. 2 to be the same as the width between the vertical line between Category 2 and the next line on the right. I mean: I am happy with that width. Now I want to make the height just the same. Must I edit my question?
    – Cromack
    Jun 15, 2017 at 9:58
0

A simple trick; specify the width of the second column explicitly, e. g. p{.7cm}, and add the same length as a vertical rule to the second row using the macro\parbox{0cm}{\rule{0cm}{\dimexpr.7cm+2\tabcolsep}}.

\documentclass{article}
\usepackage{graphicx}
\begin{document}

\newcommand*{\pad}[1]{\parbox{0cm}{\rule{0cm}{\dimexpr#1+2\tabcolsep}}}

    \begin{tabular}{c p{.7cm} c|c|}
        & & \multicolumn{2}{ c }{Category 1}                                                      \\ \cline{3-4}
        & & \multicolumn{1}{ c }{Subcat. 1\pad{.7cm}} & \multicolumn{1}{c}{Subcat. 2}             \\ \cline{3-4}
        &   \multicolumn{1}{|c|}{\rotatebox[origin=c]{90}{~Subcat. 2~}} & AAAAAAAAAA & BBBBBBBBBB \\ \cline{3-4}
        \raisebox{1cm}[0pt][0pt]{\rotatebox[origin=c]{90}{Category 2}} &
            \multicolumn{1}{|c|}{\rotatebox[origin=c]{90}{~Subcat. 1~}} & CCCCCCCCCC & DDDDDDDDDD \\ \cline{3-4}
    \end{tabular}

\end{document}

enter image description here

You must log in to answer this question.

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