4

I just learned that you can use braces to scope the effect of \tikzset. But it is not allowed in align environments:

\documentclass{article}
\usepackage{amsmath}
\usepackage{tikz}
\begin{document}
    \begin{align}
        \tikzset{every node/.style={draw}}
        \tikz{\node {};} &= \text{Box}\\
        {
            \tikzset{every node/.style={fill}}
            \tikz{\node {};} &= \text{Filled box}\\
        }
        \tikz{\node {};} &= \text{Box}\\
        \tikz{\node {};} &= \text{Box}
    \end{align}
\end{document}

This fails with a lot of errors like "Misplaced alignment tab character" and others.

Sadly, even a workaround doesn't work here:

\documentclass{article}
\usepackage{amsmath}
\usepackage{tikz}
\begin{document}
    \begin{align}
        \tikzset{every node/.style={draw}}
        \tikz{\node {};} &= \text{Box}\\
        \tikzset{every node/.style={fill}}
        \tikz{\node {};} &= \text{Filled box}\\
        \tikzset{every node/.style={draw}}
        \tikz{\node {};} &= \text{Box}\\
        \tikz{\node {};} &= \text{Box}
    \end{align}
\end{document}

enter image description here

Notice how the last node is not drawn. What's going wrong?

2 Answers 2

5

Anyhow, the &'s already limit the scopes. Hence, even removing the stand alone two braces from the OP MWE, the last two boxes are not drawn. Possible simple work-around:

either

\documentclass{article}
\usepackage{amsmath}
\usepackage{tikz}
\begin{document}
        \begin{align}
        \tikzset{every node/.style={draw}}
        \tikz{\node {};} &= \text{Box}\\
            \tikzset{every node/.style={fill}}
            \tikz{\node {};} &= \text{Filled box}\\
        \tikzset{every node/.style={draw}}\tikz{\node {};} &= \text{Box}\\
        \tikzset{every node/.style={draw}}\tikz{\node {};} &= \text{Box}
    \end{align}
\end{document}

or

\documentclass{article}
\usepackage{amsmath}
\usepackage{tikz}
\begin{document}
\tikzset{every node/.style={draw}}
        \begin{align}
        \tikz{\node {};} &= \text{Box}\\
        \tikzset{every node/.style={fill}}
            \tikz{\node {};} &= \text{Filled box}\\
        \tikz{\node {};} &= \text{Box}\\
        \tikz{\node {};} &= \text{Box}
    \end{align}
\end{document}

Both produce boxes aligned

1
  • setting the \tikzset{every node/.style={draw}} globally, before the \align and overwriting just the one that's different is nice. +1 Mar 27, 2014 at 18:50
5

within an align environment -- or any environment where cells are separated by & or \\ -- every cell is totally independent of every other. thus instructions for setting or changing styles must be complete within each cell.

you can "rescue" your workaround example by inserting the code

\tikzset{every node/.style={draw}}

immediately after the last \\.

You must log in to answer this question.

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