3

This MWE compile using lualatex with no error

\documentclass[12pt]{article}
\usepackage{graphicx}
\usepackage{tikz} 
\usetikzlibrary{matrix} 
\begin{document}
\begin{tikzpicture}
\matrix[draw=red,nodes=draw,column sep=2mm, row sep=2mm]
{
       \node (11) {};     & \node (12) {A};  & \node (13) {};  \\
       \node (21) {};     & \node (22) {B};  & \node (23) {};  \\
};

\draw  (12) -- (22);
\end{tikzpicture}
\end{document}

enter image description here

But when adding \resizebox it now gives compile error:

\documentclass[12pt]{article}
\usepackage{graphicx}
\usepackage{tikz} 
\usetikzlibrary{matrix} 
\begin{document}
 
\resizebox{.9\textwidth}{1.2\textwidth}
{%
\begin{tikzpicture}
\matrix[draw=red,nodes=draw,column sep=2mm, row sep=2mm]
{
       \node (11) {};     & \node (12) {A};  & \node (13) {};  \\
       \node (21) {};     & \node (22) {B};  & \node (23) {};  \\
};

\draw  (12) -- (22);
\end{tikzpicture}
}
\end{document}

compiling with lualatex now gives

(/usr/local/texlive/2023/texmf-dist/tex/latex/latexconfig/epstopdf-sys.cfg))
! Undefined control sequence.
<argument> \pgf@matrix@last@nextcell@options 
                                  
l.35 }
    
? 

Note that this error only shows up when using tikz matrix. I've been using \resizebox to resize tikz pics for long time with no error.

This is the first time I try \matrix and this is how I found this.

Is it possible to use \resizebox with tikz pic which has \matrix in it?

TL 2023

1 Answer 1

5

You need to define replacement for ampersands. Foe example:

\documentclass[12pt]{article}
\usepackage{graphicx}
\usepackage{tikz}
\usetikzlibrary{matrix}
\begin{document}

\resizebox{.9\textwidth}{1.2\textwidth}
{%
\begin{tikzpicture}
\matrix[draw=red,nodes=draw,column sep=2mm, row sep=2mm,
        ampersand replacement=\&
        ]
{
       \node (11) {};     \& \node (12) {A};  \& \node (13) {};  \\
       \node (21) {};     \& \node (22) {B};  \& \node (23) {};  \\
};

\draw  (12) -- (22);
\end{tikzpicture}
}
\end{document}

enter image description here

Addendum:
Your matrix can be drawn by much shorter code:

\documentclass[12pt]{article}
\usepackage{graphicx}
\usepackage{tikz}
\usetikzlibrary{matrix}
\begin{document}

\resizebox{.9\textwidth}{1.2\textwidth}
{%
\begin{tikzpicture}
\matrix (m) [matrix of nodes, nodes in empty cells,
             draw=red,
             nodes=draw,
             column sep=2mm, row sep=2mm,
            ampersand replacement=\&
            ]
{
    \&  A   \&      \\
    \&  B   \&      \\
};
\draw  (m-1-2) -- (m-2-2);
\end{tikzpicture}
}
\end{document}

Result is the same as before.

2
  • 1
    Background: TikZ makes & an active character inside a matrix which is let to \pgfmatrixnextcell. If the matrix/the picture is now used in an argment, the usual problems occur. TikZ can't change the catcode of the input anymore. (PGF actually uses \halign and real & to build the matrix internally but two & for each column.) Oct 25, 2023 at 9:20
  • @Qrrbrbirlbel, thank you for explanation.
    – Zarko
    Oct 25, 2023 at 10:30

You must log in to answer this question.

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