0

Earlier, I has asked to ask how to make &'s work for tikzcd environment nested inside tabular. I got my answer in tikz-cd's manual---I just had to use ampersand replacement=\&.

Now, another problem has cropped up (in the same context):

The code:

\begin{tabular}{rc}
    First: & $1$\\
    \hline
    Second: & \multirow{2}{*}{
        \begin{tikzcd}[ampersand replacement=\&]
            A\ar[r] \& B\ar[d]\\
            \& C
        \end{tikzcd}
    }\\
    \hline
    Third: & \\
    \hline
    Fourth: & $4$
\end{tabular}

generates

enter image description here

which is obviously not what is wanted.

Why is this happening? Is the vertical height of tikzcd's output object being diminished?

How to fix this?


Observation:

It behaves weirdly only inside \multirow. That is, the code:

\begin{tabular}{rc}
    First: & $1$\\
    \hline
    Second: & \begin{tikzcd}[ampersand replacement=\&]
        A\ar[r] \& B\ar[d]\\
        \& C
    \end{tikzcd}\\
    \hline
    Third: & \\
    \hline
    Fourth: & $4$
\end{tabular}

produces, as expected,

enter image description here

But I want the second and third rows of the second column merged with the diagram being centered in that merged cell.


The desired result:

enter image description here

6
  • And then you look up ampersand in the tikz-cd manual, where it is explained what to do if you get this error.
    – daleif
    Apr 8 at 7:47
  • Okay, so I encountered another problem, I will edit my post to include that.
    – Atom
    Apr 8 at 8:12
  • Please extend your examples to complete documents, that makes it easier for others to copy and test.
    – daleif
    Apr 8 at 8:34
  • You would need to put some empty rows after the tikzcd to make space for it. Or at least rows with empty cells in the second column. Apr 8 at 8:42
  • But actually, it would be more logical to put Second: in a \multirow and not the tikzcd in your example. Apr 8 at 9:04

3 Answers 3

3

Like this?

\documentclass{article}
\usepackage{tikz-cd, multirow}
\usepackage{makecell}

\begin{document}

\begin{tabular}{rc}
    First: & $1$\\
    \hline
    \makecell[cr]{Second:\\Third:} & \makecell{%
        \begin{tikzcd}[ampersand replacement=\&]
            A\ar[r] \& B\ar[d]\\
            \& C
        \end{tikzcd}}\\
      \hline
    Fourth: & $4$
\end{tabular}

\end{document}

enter image description here

4
  • Sorry, I meant second and third column of the second row to be merged. Apologies for the confusion. Editing it.
    – Atom
    Apr 8 at 9:57
  • You have only two columns. Maybe you should add a sketch of the desired result. Apr 8 at 23:04
  • yea, I interchanged the 'column' and 'row' above. I created a lot of confusion, and I sincerely apologize for that. Your most recent answer address this. Just one thing: Can you get the Second: and Third: stick together close to each other (skipping a space of row between them; that's why I used \multirow)? I will attach a sketch of the desired result.
    – Atom
    Apr 9 at 4:43
  • Thanks for persisting with me! You saved the day(s)!
    – Atom
    Apr 9 at 9:51
1

You get more precise alignment without multirow.

\documentclass{article}
\usepackage{amsmath}
\usepackage{tikz-cd}

\begin{document}

\begin{tabular}{rc}
  First: & $1$\\
  \hline
  \begin{tabular}{@{}r@{}} Second: \\ Third: \end{tabular} &
  \begin{tabular}{@{}c@{}}
    \begin{tikzcd}
    A\ar[r] & B\ar[d]\\
            & C
    \end{tikzcd}
    \end{tabular}
  \\
  \hline
  Fourth: & $4$
\end{tabular}

\end{document}

Do you see the trick? Nesting tabular.

enter image description here

2
  • Interesting. I have never seen this use of nested tabulars before. Thanks!
    – Atom
    Apr 9 at 9:53
  • In fact that is what \makecell does. Makecell is just a higher abstraction level. Apr 9 at 15:20
0

With tabularray package and in tikzcd image use of ampersand replacement \&:

\documentclass{article}
\usepackage{tabularray}
\usepackage{tikz-cd}

\begin{document}

    \begin{tblr}{colspec={rc},
                 vspan=even
                 }
First:  &    1              \\  \hline
Second: & \SetCell[r=2]{c}  % <--- tabularray syntax for multi row cell 
            \begin{tikzcd}[ampersand replacement=\&]
            A \rar  \& B \dar   \\
                    \& C
            \end{tikzcd}    \\
Third:  &                   \\  \hline
Fourth: &   4 
    \end{tblr}

\end{document}

enter image description here

Addendum: From your edited question follows, that you not need multirow cell, but text in the cell adjacent to cell with image with text in two lines. This is simple achieved by use of tabularray:

\documentclass{article}
\usepackage{tabularray}
\usepackage{tikz-cd}

\begin{document}
    \begin{tblr}{colspec={Q[r,m]Q[c,m]}}
First:  &    1              \\  \hline
{Second:\\ Third:}
        &   \begin{tikzcd}[ampersand replacement=\&]
            A \rar  \& B \dar   \\
                    \& C
            \end{tikzcd}    \\  \hline
Fourth: &   4
    \end{tblr}
\end{document}

enter image description here

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.