[Tex/LaTex] Label variables of equation using tikz without using itemize

equationslabelstikz-pgf

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?

Thanks,

enter image description here

Best Answer

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.)

\documentclass{article}

\usepackage{tikz}
\usetikzlibrary{matrix}
\usetikzlibrary{positioning}

\begin{document}

\begin{tikzpicture}

    \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);


\end{tikzpicture}

\end{document}

enter image description here