6

I've made a diagram in tikz, see below, and after some messing around I've been able to align all nodes properly. I say 'messing around' because I find the documentation impossible to navigate, so I've resorted to randomly trying out commands and syntax for this. A problem I'm unable to resolve though, is how to align the edges in the diagram:

enter image description here

I would like the edges on the right to connect $\overline{1}$ to $\overline{H}$, and $\overline{H}$ to $\overline{G}$, with the expressions $=H/N$ and $=G/N$ sticking out to the side. Here is my code so far:

\documentclass{report}
\usepackage[dutch]{babel}
\usepackage{tikz}

\begin{document}
\begin{tikzpicture}
\node at (1.0,4.5)  []      (A1) {$G$};
\node at (2.5,4.5)  [right] (B1) {$\overline{G}=G/N$};
\node at (1.0,3.0)  []      (A2) {$H$};
\node at (2.5,3.0)  [right] (B2) {$\overline{H}=H/N$};
\node at (1.0,1.5)  []      (A3) {$N$};
\node at (2.5,1.5)  [right] (B3) {$\overline{1}$};
\node at (1.0,0.0)  []      (A4) {$1$};

\path[-stealth] (A1) edge node[above] {$\pi$} (B1)
                     edge[-] (A2)
                (A2) edge node[above] {} (B2)
                     edge[-] (A3)
                (A3) edge node[above] {} (B3)
                     edge[-] (A4)
                (B1) edge[-] (B2)
                (B2) edge[-] (B3);
\end{tikzpicture}
\end{document}
6

I always use a matrix of math nodes for diagrams like this (some people prefer the tikzcd package). This allows you to place the objects in your diagram in a matrix/array and then add arrows at will.

For your diagram I wrote type:

\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{matrix}

\begin{document}

  \begin{tikzpicture}[looseness=.5,auto]
    \matrix (M)[matrix of math nodes,row sep=1cm,column sep=16mm]{
        G & \overline{G}=G/N\\
        H & \overline{H}=H/N\\
        N & \overline{1}\\
        1\\
     };
     \draw[->] (M-1-1)--node{$\pi$}(M-1-2);
     \foreach \x [evaluate=\x as \xx using int(\x+1)] in {1,2} {
        \draw(M-\x-1)--(M-\xx-1);
        \draw(M-\x-2)--(M-\xx-2);
     }
    \draw[->](M-2-1)--(M-2-2);
    \draw[->](M-3-1)--(M-3-2);
    \draw[->](M-3-1)--(M-4-1);
  \end{tikzpicture}

\end{document}

which produces:

enter image description here

If you are already familiar with tikz then this is mostly self-explanatory. The most interesting point is that the (M) after the \matrix tells tikz that the nodes in the matrix can be referred to using M-<row>-<col>. You can change (M) to anything you like.

EDIT

I personally, like the alignment in the diagram above but I accept that this is not to everyone's tastes. By adding a little bit of styling it is possible to move the lines on the right-hand side of the diagram. Styling of nodes inside a matrix can be achieved by putting it inside |[<style>]| -- that is, by giving an optional argument [<style>] to the implicit node command that it used for the matrix entries (and then enclosed in |...|). Using styling we can make the node left aligned and wide enough. After this we can tweak the vertical lines on the right-hand side to produce:

enter image description here

Here is the modified code:

\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{matrix}

\begin{document}

  \begin{tikzpicture}[looseness=.5,auto,
        leftnodes/.style={text width=3em, align=left}]
    \matrix (M)[matrix of math nodes,row sep=1cm,column sep=16mm]{
        G & |[leftnodes]|\overline{G}=G/N\\
        H & |[leftnodes]|\overline{H}=H/N\\
        N & |[leftnodes]|\overline{1}\\
        1\\
     };
     \draw[->] (M-1-1)--node{$\pi$}(M-1-2);
     \draw(M-1-1)--(M-2-1);
     \draw([xshift=2mm]M-1-2.south west)--([xshift=2mm]M-2-2.north west);
     \draw(M-2-1)--(M-3-1);
     \draw([xshift=2mm]M-2-2.south west)--([xshift=2mm]M-3-2.north west);
    \draw[->](M-2-1)--(M-2-2);
    \draw[->](M-3-1)--(M-3-2);
    \draw[->](M-3-1)--(M-4-1);
  \end{tikzpicture}

\end{document}

A much simpler way of doing this is simply to put the "offending" parts of the equations inside \rlap{$...$}, which essentially tells LaTeX to typeset them but leave the cursor alone. With this minor change, the code becomes:

\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{matrix}

\begin{document}

  \begin{tikzpicture}[looseness=.5,auto]
    \matrix (M)[matrix of math nodes,row sep=1cm,column sep=16mm]{
        G & \overline{G}\rlap{$=G/N$}\\
        H & \overline{H}\rlap{$=H/N$}\\
        N & \overline{1}\\
        1\\
     };
     \draw[->] (M-1-1)--node{$\pi$}(M-1-2);
     \foreach \x [evaluate=\x as \xx using int(\x+1)] in {1,2} {
        \draw(M-\x-1)--(M-\xx-1);
        \draw(M-\x-2)--(M-\xx-2);
     }
    \draw[->](M-2-1)--(M-2-2);
    \draw[->](M-3-1)--(M-3-2);
    \draw[->](M-3-1)--(M-4-1);
  \end{tikzpicture}

\end{document}

and the output is the same as in the second image above.

  • This is a nice approach. However, this doesn't address the proper alignment of the rightmost column, which is the essence of the problem. – Servaes Dec 10 '16 at 13:39
  • 1
    @Servaes I actually like this alignment:) But as you're not, and because it's your question, I have shown how to change this using a matrix of nodes. – Andrew Dec 10 '16 at 14:44
  • Thank you for taking the time to expand your answer so thoroughly. I like your last solution best, not only because it is simpler, but also because the alignment is better there (the edges are a bit far left in the other solution). It works perfectly! – Servaes Dec 10 '16 at 17:40
6

A quick solution:

Load the mathtools package \usepackage{mathtools} and use \rlap to let the right part of the equations not count to the bounding box of the node:

\node at (2.5,4.5)  [right] (B1) {$\overline{G}\mathrlap{{}=G/N}$};
\node at (2.5,3.0)  [right] (B2) {$\overline{H}\mathrlap{{}=H/N}$};

However, this has some drawbacks. $\overline{1}$ and \overline{H} do not have the same width, so the edges will not be exactly vertical. To circumvent this, you can set the text width of all nodes to the same value with [text width=1em,align=center]:

\documentclass{report}
\usepackage{tikz}
\usepackage{mathtools}

\begin{document}
    \begin{tikzpicture}
        \tikzset{symbol/.style={text width=1em,align=center}}
        \node at (1.0,4.5)  [symbol]      (A1) {$G$};
        \node at (2.5,4.5)  [symbol,right] (B1) {$\overline{G}\mathrlap{{}=G/N}$};
        \node at (1.0,3.0)  [symbol]       (A2) {$H$};
        \node at (2.5,3.0)  [symbol,right] (B2) {$\overline{H}\mathrlap{{}=H/N}$};
        \node at (1.0,1.5)  [symbol]       (A3) {$N$};
        \node at (2.5,1.5)  [symbol,right] (B3) {$\overline{1}$};
        \node at (1.0,0.0)  [symbol]       (A4) {$1$};

        \path[-stealth] (A1) edge node[above] {$\pi$} (B1)
            edge[-] (A2)
            (A2) edge node[above] {} (B2)
            edge[-] (A3)
            (A3) edge node[above] {} (B3)
            edge[-] (A4)
            (B1) edge[-] (B2)
            (B2) edge[-] (B3);
    \end{tikzpicture}
\end{document}

4

I feel it's a typical tikz-cd usage case, so, I suggest the following:

\documentclass{report}
\usepackage[dutch]{babel}
\usepackage{tikz-cd}    
\begin{document}

\[
\begin{tikzcd}
G \arrow[r,"\pi"]\arrow[dash,d] & \overline{G}=G/N \arrow[dash,d] \\
H \arrow[r] \arrow[dash,d]      & \overline{H}=H/N \arrow[dash,d] \\
N \arrow[r] \arrow[dash,d]      & \overline{1}                    \\
1
\end{tikzcd}
\]

\end{document}

enter image description here

If you still prefer the left-alignment (certainly I don't, but, it is your question) you can play with the tikzcd a little like this:

\[
\begin{tikzcd}
G \arrow[r,"\pi"]\arrow[dash,d] & \arrow[dash,d] \mathrlap{\overline{G}=G/N} \\
H \arrow[r] \arrow[dash,d]      & \arrow[dash,d] \mathrlap{\overline{H}=H/N} \\
N \arrow[r] \arrow[dash,d]      & \mathrlap{\overline{1}}                    \\
1
\end{tikzcd}
\]

enter image description here

  • This doesn't address the proper alignment of the rightmost column, which is the essence of the problem. Also, I tried installing the tikz-cd package before but I have never managed to succeed. – Servaes Dec 10 '16 at 13:42
  • @Servaes -- Having troubles installing the tikzcd is totally another issue for which you're welcome to submit a separate question. For your current question, I updated my answer to fulfill your needs. – AboAmmar Dec 10 '16 at 16:03
3

You could use xshift to shift the endpoints of the connecting lines:

([xshift=-18pt]B1.south) edge[-] ([xshift=-19pt]B2.north)
([xshift=-19pt]B2.south) edge[-] (B3)

Another option could be to split the nodes into two parts (the following code is based on the positioning library to avoid writing exact coordinates):

\documentclass{standalone}
\usepackage{tikz}
\usetikzlibrary{positioning}

\begin{document}
\begin{tikzpicture}[node distance=1.5cm] % standard distance between nodes
\node                      (A1) {$G$};
\node [right of=A1]        (B1) {$\overline{G}$};
\node [right=0.25cm of B1] (C1) {$= G/N$}; % second part of B1
\node [below of=A1]        (A2) {$H$};
\node [below of=B1]        (B2) {$\overline{H}$};
\node [right=0.25cm]       (C2) {$= H/N$}; % second part of B2
\node [below of=A2]        (A3) {$N$};
\node [below of=B2]        (B3) {$\overline{1}$};
\node [below of=A3]        (A4) {$1$};

\path[-stealth] (A1) edge node [above] {$\pi$} (B1)
                     edge[-] (A2)
                (A2) edge (B2)
                     edge[-] (A3)
                (A3) edge (B3)
                     edge[-] (A4)
                (B1) edge[-] (B2)
                (B2) edge[-] (B3);
\end{tikzpicture}
\end{document}
  • This feels a bit clumsy, but I like it anyway. – Servaes Dec 10 '16 at 13:42

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.