1

I have a TikZ matrix, and within each node, I have a set of math equations. Using align*, the alignment is off and shows, compared to the usual behavior, some extra spacing to the left of the &. How can this be fixed?

Additional question: can the text width command adapted in a way that it is just the width of the block of formulas?

The MWE shows the typical align* on top and then the TikZ that ought to be fixed. Other blocks of the matrix are left out to reduce complexity.

\documentclass{article}
\usepackage{amsmath}
\usepackage{tikz}
\begin{document}

It should look like this:
\begin{align*}
x, y &>0\\
x&\geq \gamma  \\
x' = y'&= 0
\end{align*}

But it looks like this:
\begin{tikzpicture}
    [auto,
    block/.style ={rectangle, draw, thick,
                    text width=7em, align=center, rounded corners,
                    minimum height=4em}]
    \matrix [column sep=18mm, row sep=15mm]
{
    % row 1
    \node [block]  (init)   {\texttt{init}
                    \begin{align*}
                    x, y &>0\\
                    x&\geq \gamma  \\
                    x' = y' &= 0        
                    \end{align*}
                    }; 
                    &  \\
};
\end{tikzpicture}
\end{document}
1
  • Use the aligned or the alignedat environment inside math mode to not have to deal with text width. Commented Aug 5 at 8:38

2 Answers 2

2

You don't have to guess the widths if you use aligned (and also gathered in order to get good spacing). Since you want to use & inside a node, you must replace the alignment symbol for the TikZ matrix.

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

\begin{document}

\begin{align*}
x, y &>0\\
x&\geq \gamma  \\
x' = y'&= 0
\end{align*}

\begin{tikzpicture}[
  auto,
  block/.style ={
    rectangle,
    draw,
    thick,
    text width=7em,
    align=center,
    rounded corners,
    minimum height=4em
  },
]
  \matrix [ampersand replacement=\&] {
    % row 1
    \node [block] (init) {
      $\begin{gathered}
        \texttt{init} \\
        \begin{aligned}
          x, y &>0 \\
          x&\geq \gamma  \\
          x' = y' &= 0        
        \end{aligned}
      \end{gathered}$
    };
   \& \\
  };
\end{tikzpicture}

\end{document}

enter image description here

1
  • Thanks @egreg, this even helps with the bonus question. Very helpful!
    – freshens
    Commented Aug 7 at 6:17
5

You should some how distinguish ampersands belonging to align* environment from ampersands belonging to matrix:

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

\begin{document}
It should look like this:
\begin{align*}
x, y    & >0            \\
x       &\geq \gamma    \\
x' = y' &= 0
\end{align*}

However now looks like this:

    \begin{tikzpicture}[
block/.style = {draw, thick, rounded corners, 
                text width=7em, align=center, minimum height=4em}]
\matrix [column sep=18mm, row sep=15mm,
         ampersand replacement=\&]  % <---
{
    % row 1
\node [block]  (init)   {\texttt{init}
                \begin{align*}
                x, y    & >0            \\
                x       & \geq \gamma   \\
                x' = y' & = 0        
                \end{align*}
                }; 
                    \&  \\          % <---
};
    \end{tikzpicture}
\end{document}

enter image description here

3
  • Wow, this is very elegant. I would have never imagined that an ampersand would be the source of the issue. Thank you, @zarko!
    – freshens
    Commented Aug 5 at 8:51
  • @freshens, sometimes help to read package documentation 😉. But documentation is huge and it is easy overlooked some details ... See TikZ and PGF manual, section 20.5 Considerations Concerning Active Characters, page 327.
    – Zarko
    Commented Aug 5 at 9:00
  • Yes, yes. I did before I posted - but that documentation is just k***ing me. Especially, if I do not know, what exactly I am looking for ;-) Ampersand would have been my final guess.
    – freshens
    Commented Aug 5 at 9:11

You must log in to answer this question.

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