I'm trying to use tikz to label the variables of an equation. I know of a similar question (Itemized Equation) but the answer uses itemize to label the variables. I would like to do something similar to what the attached figures shows, though of course, I would like for it to look nice :) Any suggestions?


enter image description here


Another solution is to use a matrix. This allows you to put the equation in order easily, and then, if you define the cells of the matrix to be nodes, you can then use the positioning library to position your labels relative to those nodes.

(Note that the shorten >= option is used in this code to make the arrows look a bit better, so they don't immediately brush up against the lines of the individual characters. You can modify as you desire.)





    \matrix[name=M1, matrix of nodes, inner sep=0pt, column sep=0pt]{
      \node (Y1) {Y}; & \node (equals) {=}; & \node (Y2) {Y}; & ( & \node (X) {X,}; & \node (Z) {Z,}; & \node (W) {W;}; & \node (A) {A}; & ) \\

    \node (Output) [left=2.5em of Y1] {Output};
    \node (VariableZ) [above=2.5em of Z] {Variable Z};
    \node (VariableW) [below=2.5em of W] {Variable W};
    \node (VariableX) [left=2em of VariableZ] {Variable X};
    \node (ParameterA) [right=2em of VariableZ] {Parameter A};

    \draw[->] (Output) -- (Y1);
    \draw[->, shorten >=0.1em] (VariableZ) -- (Z);
    \draw[->, shorten >=0.2em] (VariableX) -- (X.north);
    \draw[->, shorten >=0.1em] (ParameterA) -- (A.north);
    \draw[->] (VariableW) -- (W);



enter image description here


Here's something to get you started, based on the tried and tested tikzmark idea- you'll see it used in lots of answers on this site, originally published in Adding a large brace next to a body of text


% arara: pdflatex
% !arara: indent: {overwrite: on}
\usepackage{amsmath} % loaded automatically by beamer

\newcommand{\tikzmark}[3][]{\tikz[overlay,remember picture,baseline] \node [anchor=base,#1](#2) {#3};}

    \mathcal{A} = (\,\tikzmark{identity}{\texttt{I}} -\tikzmark[red]{G}{\texttt{G}}\,\,\, 
    \tikzmark[blue]{L}{\texttt{L}} - \tikzmark[purple]{C}{\texttt{C }}\,)
\begin{tikzpicture}[overlay, remember picture,node distance =1.5cm]
    \node (identitydescr) [below left=of identity ]{words};
    \draw[,->,thick] (identitydescr) to [in=-90,out=90] (identity);
    \node[red] (Gdescr) [below =of G]{other words};
    \draw[red,->,thick] (Gdescr) to [in=-90,out=90] (G);
    \node[blue,xshift=1cm] (Ldescr) [above right =of L]{some words};
    \draw[blue,->,thick] (Ldescr) to [in=45,out=-90] (L.north);
    \node[purple] (Cdescr) [below right =of C]{more words};
    \draw[purple,->,thick] (Cdescr) to [in=-90,out=90] (C.south);
  • This is kind of nice, however I have the problem that text is only moved around the equation but the annotations get printed right on top of it. Do you have an idea how to prevent this?
    – MatoBehr
    Apr 9 '15 at 13:53
  • @MatoBehr it's difficult to say without an example; perhaps you can post a new question that demonstrates your problem?
    – cmhughes
    Apr 9 '15 at 14:20
  • I only added text to your example. I have posted it as a new question now. Thanks
    – MatoBehr
    Apr 9 '15 at 14:53

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.