# Beamer: tikZ-matrix with nodes having multiple lines AND variable width

I have a slide of the following style: there is a tikz-matrix of two columns, the right column requiring TWO properties: 1) it should allow nodes with multiple lines of text/math, 2) the nodes shouldn't all be fixed width.

Earlier, I had got a great solution to the first feature above, which was to use text width attribute to describe the nodes. However, now, I find that this would not let me have the flexibility I need in part 2) above. The reason I want 2) is because when the expressions within these right hand side nodes aren't that long, the boxes look a bit ugly if they are still that wide. Any help would be much appreciated. Thanks!


\documentclass{beamer}
\mode<presentation>
{
\setbeamertemplate{caption}[numbered]
}
%%%%%%%%%
\usepackage{tikz}
\usetikzlibrary{fit,overlay-beamer-styles,tikzmark, matrix}
\usepackage{tcolorbox}

\begin{document}
\section{Proofs}

\begin{frame}[fragile]
\frametitle{Our Proof Outline}

\centering
\begin{tikzpicture}[optimization problem/.style = {%
rounded corners,
draw = green,
inner ysep=5pt,
inner xsep=2pt,
align = left},]

\matrix[matrix of nodes,row sep=1em,column sep=.5em,
nodes={anchor=west,
visible on=<\the\pgfmatrixcurrentrow->,},
column 2/.style={nodes={optimization problem,
text width = 16 em,
}}](M){
Consider: & some function \\
Want to compare: & {one function and\\  a much longer function}\\
Our interpolating function: & a really really long mathematical expression here\\
Observe: & {observation 1 \\ observation 2 }\\
};

\end{tikzpicture}
\end{frame}

\end{document}



This is still LaTeX, so you can use a tabular to have two rows of variable width.

\documentclass{beamer}
\mode<presentation>
{
\setbeamertemplate{caption}[numbered]
}
%%%%%%%%%
\usepackage{tikz}
\usetikzlibrary{fit,overlay-beamer-styles,tikzmark, matrix}
\usepackage{mathtools}

\begin{document}
\section{Proofs}

\begin{frame}[fragile]
\frametitle{Our Proof Outline}

\centering
\begin{tikzpicture}[optimization problem/.style = {%
rounded corners,
draw = green,
inner ysep=5pt,
inner xsep=2pt,
align = left},]

\matrix[matrix of nodes,row sep=1em,column sep=.5em,
nodes={anchor=west,
visible on=<\the\pgfmatrixcurrentrow->,},
column 2/.style={nodes={optimization problem}}](M){
Consider: & some function\\
Want to compare: & \begin{tabular}{@{}l@{}}
one function and\\
a much longer function\end{tabular}\\
Our interpolating function: &
{$\begin{lgathered} E=mc^2\\ \quad=f=g=h=k \end{lgathered}$}\\
Observe: & \begin{tabular}{@{}l@{}}
observation 1\\
observation 2%
\end{tabular}\\
};

\end{tikzpicture}
\end{frame}
\end{document}


Note:

• One needs to be a bit careful with the spaces at the end of the cells before \\.
• You can use environments with & in if you use an ampersand replacement, but this is not needed here. (This has been used in the later answer by Zarko.)

Here is an example with an aligned environment and symmetric spacing. (The spacing at the left and right is set by inner xsep=2pt, of optimization problem, which I increased here to 4pt.)

\documentclass{beamer}
\mode<presentation>
{
\setbeamertemplate{caption}[numbered]
}
%%%%%%%%%
\usepackage{tikz}
\usetikzlibrary{fit,overlay-beamer-styles,tikzmark, matrix}
\usepackage{mathtools}

\begin{document}
\section{Proofs}

\begin{frame}[fragile]
\frametitle{Our Proof Outline}

\centering
\begin{tikzpicture}[optimization problem/.style = {%
rounded corners,
draw = green,
inner ysep=5pt,
inner xsep=4pt,
align = left},
]

\matrix[matrix of nodes,row sep=1em,column sep=.5em,
nodes={anchor=west,
visible on=<\the\pgfmatrixcurrentrow->,},
column 2/.style={nodes={optimization problem}},
ampersand replacement=\&](M){
Consider: \& some function\\
Want to compare: \& \begin{tabular}{@{}l@{}}
one function and\\
a much longer function\end{tabular}\\
Our interpolating function: \&
{\begin{aligned} E&=mc^2\\ &=f=g=h=k \end{aligned}}\\
Observe: \& \begin{tabular}{@{}l@{}}
observation 1\\
observation 2%
\end{tabular}\\
};

\end{tikzpicture}
\end{frame}
\end{document}


• Thank you so much! So, in the above example, the "really, really long mathematical expression here" needs to be replaced by an actually long mathematical expression (so I anticipate using \multline. How would that work in this code? – lazulikid May 30 at 2:42
• @lazulikid I added a multline* environment. Except for the fact that it starts a new line that one needs to undo in one way or another there is no problem. – user194703 May 30 at 2:55

Similar idea as at @Schrödinger's cat answer: where is needed multi line text in matrix's cells, is used \makecell command (which essential contain tabular environment) from the makecell package and math environments defined in amsmath packages (loaded by beamer.

For proper work of ampersands in aligned math, for matrix is used ampersand replacement=\& for separating of matrix cells:

\documentclass{beamer}
\mode<presentation>
{
\setbeamertemplate{caption}[numbered]
}
%%%%%%%%%
\usepackage{tikz}
\usetikzlibrary{matrix, overlay-beamer-styles }
%\usepackage{tcolorbox}
\usepackage{makecell}

\begin{document}
\section{Proofs}
\begin{frame}[fragile]
\frametitle{Our Proof Outline}
\centering
\begin{tikzpicture}
\matrix (M) [ampersand replacement=\&,
matrix of nodes,
column sep=.5em, row sep=1em,
nodes={anchor=west,
visible on=<\the\pgfmatrixcurrentrow->},
column 2/.style={nodes={rounded corners,
draw = green,
inner ysep=5pt, inner xsep=2pt,
minimum width=4 em}}
]
{
Consider:   \& some function \\
Want to compare:
\& \makecell[l]{one function and\\ a much longer function}   \\
Our interpolating function:
\& \begin{aligned} c^2 & = a^2 + b^2 \\ a & = \sqrt{c^2-b^2} \end{aligned}   \\
Observe:    \& \makecell[l]{observation 1\\ observation 2}   \\
};
\end{tikzpicture}
\end{frame}
\end{document}


which final result is: