34

I make a lot of commutative diagrams and am trying to switch to Tikz from amcd,diagrams, etc. I often have to refer to these diagrams in the text and would rather have them as equations (i.e. with and equation number) rather than a figure. I have tried just putting it in an equation environment but it neither centers the diagram (as ma)

2
  • Please explain more why putting them as figures is no good. Presumably because you want to refer to commutative diagrams as if they were equations? Buy why can't you just refer to figures instead?
    – Seamus
    Commented Oct 20, 2010 at 12:21
  • 4
    @Seamus: In a math paper, commutative diagrams are functionally like equations, so the coherent thing is to label both in the same way. In a math paper, in which one customarily refers to dozens, if not hundreds, of equations, writing "Figure 32" instead of (32) is cumbersome. Also the label "Figure 32" takes up more (vertical) space than does a margin equation number. Surely there are still other reasons, but those are what occur to me.
    – Dan Fox
    Commented Sep 10, 2013 at 16:32

4 Answers 4

42
\begin{equation}
\begin{tikzpicture}[baseline=(current  bounding  box.center)]
......
\end{tikzpicture}
\end{equation}

Copied from Mtikz.pdf.

8
  • 4
    That's really slick---the [baseline=(current bounding box.center)] centers the equation reference number, rather than having it at the bottom of the picture. Commented Oct 20, 2010 at 18:02
  • Exactly. "equation" puts the reference number on the baseline of the picture, so the solution is to move the baseline.
    – Mephisto
    Commented Oct 20, 2010 at 18:55
  • And it works for all sorts of other things, too, like labelling for lists! that's great---thanks! Commented Oct 20, 2010 at 19:59
  • Could the example about be edited to include \hbox, then the answer would be complete?
    – user1536
    Commented Oct 21, 2010 at 8:53
  • I don't understand your comment --- there is no need for \hbox. Did you even try it?
    – Mephisto
    Commented Oct 21, 2010 at 13:39
5

just place the TikZ picture into a box, for example using \text{} (which is an AMS command).

In case you do not use AMS, you can also use \hbox (which is a plain TeX command) instead.

Example which compiles without extra packages:

\begin{equation}
\hbox{\tikz\draw[draw=black,fill=red] (0,0) circle (5pt);}
\label{eq:test}
\end{equation}

\noindent
In (\ref{eq:test}), we see a TikZ picture.\dotfill

Christian

6
  • This is still using a distinct counter from the one used in the equation environment, so this might not work how the OP wanted it to...
    – Seamus
    Commented Oct 20, 2010 at 13:16
  • 1
    @Seamus: How is the counter distinct? @user1537: \eqref handles the parentheses.
    – TH.
    Commented Oct 20, 2010 at 13:51
  • Thanks , hbox works for generating an appropriate equation number, but I am using the tikzpicture environment, which seems to conflict with \text{} which for some reason it doesnt like and the equation number is not placed correctly (at the bottom of the diagram rather than the middle).
    – user1536
    Commented Oct 20, 2010 at 14:17
  • @user1536: Use \vcenter instead of \hbox.
    – TH.
    Commented Oct 20, 2010 at 15:21
  • @TH Sorry, I didn't notice that you'd wrapped the hbox in an equation environment, I thought hbox had its own counter, that was distinct. I see now what you mean...
    – Seamus
    Commented Oct 20, 2010 at 16:26
2

If your Tikz image lives inside a standalone file, Mephisto's answer will not work since baseline modifications do not carry over to the main document, unfortunately.

Another option that will work in such cases is to wrap the Tikz image in amsmath's aligned environment.

\begin{equation}
\begin{aligned}
    \includestandalone{<tikzimage>}
\end{aligned}
\end{equation}

If this is used frequently (as OP states) one should consider wrapping this construction inside a custom environment.

\newenvironment{alignedeqn}
    {\begin{equation}\begin{aligned}}
    {\end{aligned}\end{equation}\ignorespacesafterend}
1

To put the equation number on top or bottom (which is the default) just use the following commands:

\begin{equation}
\begin{tikzpicture}[baseline=(current  bounding  box.north)]
....
\end{tikzpicture}
\end{equation}

or

\begin{equation}
\begin{tikzpicture}[baseline=(current  bounding  box.south)]
....
\end{tikzpicture}
\end{equation}

which is the default.

2
  • what if I want the equation number to be in the middle?
    – Wallflower
    Commented Aug 18, 2022 at 9:48
  • Nevermind box.east, did the job haha ;) Thanks anyway!
    – Wallflower
    Commented Aug 18, 2022 at 9:49

You must log in to answer this question.

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