1

How can I align the equations in this diagram? I have tried using the align environment but it doesn't work. Here is the code:

\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{matrix}
\usetikzlibrary{matrix,positioning}
\tikzset{bullet/.style={circle,fill,inner sep=2pt}}
\usepackage{amsmath}
\usepackage{amsfonts}
\usepackage{amssymb}
\begin{document}
\begin{figure}[h]
    \centering
    \caption{hktkrkrkrrrkrkrkrkrk}
    \begin{tikzpicture}[>=stealth,sloped]
        \matrix (tree) [%
        matrix of nodes,
        minimum size=.2cm,
        column sep=1cm,
        row sep=.2cm,
        ]
        {
            &   & \(
            \begin{aligned}
                S_{uu} &= \$19.8375\\[-4pt]
                C_{uu} &= \$3.83750\\
            \end{aligned}\) \\
            & \(
            \begin{aligned}
                S_{u} &= \$17.25\\[-4pt]
                C_{u} &= \$1.876093565\\
                \Delta &= .88985507250
            \end{aligned}\) &   \\
            \(
            \begin{aligned}
                S_{0} &= \$15\\
                C_{0} &= \$.9171927207\\
                \Delta &= .50029161740
            \end{aligned}\)
            &   & \(
            \begin{aligned}
                S_{ud} &= \$15.525\\[-4pt]
                C_{ud} &= \$0\\
            \end{aligned}\) \\
            & \(
            \begin{aligned}
                S_{d} &= \$13.5\\[-4pt]
                C_{d} &= \$0\\
                \Delta &= 0
            \end{aligned}\) &   \\
            &   & \(
            \begin{aligned}
                S_{dd} &= \$12.15\\[-4pt]
                C_{dd} &= \$0\\
            \end{aligned}\) \\
        };
        \draw[-] (tree-3-1) -- (tree-2-2) node [midway,above] {};
        \draw[-] (tree-3-1) -- (tree-4-2) node [midway,below] {};
        \draw[-] (tree-2-2) -- (tree-1-3) node [midway,above] {};
        \draw[-] (tree-2-2) -- (tree-3-3) node [midway,below] {};
        \draw[-] (tree-4-2) -- (tree-3-3) node [midway,above] {};
        \draw[-] (tree-4-2) -- (tree-5-3) node [midway,below] {};
    \end{tikzpicture}
\end{figure}
\end{document}

Please I am kindly asking for your help on how to fix the issue.enter image description here

1 Answer 1

2

Do you looking for such diagram?

enter image description here

Edit: You need to replace ampersands in matrix with for example \& that they can be distinguished from ampersands in nodes. Beside this in your particular case, for perfectly aligned equations in matrix columns, you need to anchor nodes to the west too:

\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{matrix,positioning}
\tikzset{bullet/.style={circle,fill,inner sep=2pt}}

\usepackage{amsmath, amssymb}
\usepackage{siunitx}

\begin{document}
    \begin{figure}[h]
    \centering
    \caption{hktkrkrkrrrkrkrkrkrk}
\begin{tikzpicture}
\matrix (tree) [matrix of math nodes,
                nodes = {anchor=west},
                column sep=1cm,
                row sep=.2cm,
                ampersand replacement=\&
                ]
{
    \&   \& \begin{aligned}
            S_{uu} & = \$ 19.8375  \\
            C_{uu} & = \$  3.83750 \\
          \end{aligned}                     \\
    \& \begin{aligned}
        S_{u}  & = \$17.25\\ 
        C_{u}  & = \$1.876093565\\
        \Delta & = .88985507250 \\
            \end{aligned}   \&              \\
\begin{aligned}
    S_{0} &= \$15\\
    C_{0} &= \$.9171927207\\
    \Delta &= .50029161740
\end{aligned}   \& \& \begin{aligned}
                        S_{ud} &= \$15.525\\%[-4pt]
                        C_{ud} &= \$0\\
                      \end{aligned}     \\
    \& \begin{aligned}
        S_{d} &= \$13.5\\[-4pt]
        C_{d} &= \$0\\
        \Delta &= 0
     \end{aligned}  \&                  \\
    \&  \& \begin{aligned}
                    S_{dd} &= \$12.15\\[-4pt]
                    C_{dd} &= \$0\\
               \end{aligned}                \\
        };
\draw   (tree-3-1) -- (tree-2-2)
        (tree-3-1) -- (tree-4-2)
        (tree-2-2) -- (tree-1-3)
        (tree-2-2) -- (tree-3-3)
        (tree-4-2) -- (tree-3-3)
        (tree-4-2) -- (tree-5-3);
\end{tikzpicture}
    \end{figure}
\end{document}
7
  • I want the output to be such that all the equations in each column are perfectly aligned like what you did for column 1 and column 3. Can you help by adjusting the code?
    – itc
    Aug 6, 2021 at 2:31
  • @it, this is not clear from your question (at least too me).
    – Zarko
    Aug 6, 2021 at 2:33
  • can you please help by making all equations perfectly aligned like what you did in column 3. Please I need help
    – itc
    Aug 6, 2021 at 2:36
  • @itc, they can not be perfectly aligned since they are independently placed in matrix nodes. In edited answer you can find quite good approximation of what you like to have.
    – Zarko
    Aug 6, 2021 at 2:44
  • Thank you very much @Zarko.
    – itc
    Aug 6, 2021 at 3:01

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.