1

I have the following commutative diagram in TikZ:

        \begin{tikzpicture}
            \matrix (b) [matrix of math nodes, row sep=2em, column sep=2em]
            {
                D(0) & D(1) & \cdots & C \\
                C & C & \cdots & C \\
            };
            \path[->, font=\scriptsize]
            (b-1-1) edge node[auto]{\(\scriptstyle{g_0}\)} (b-1-2) edge node[auto]{\(\scriptstyle{\phi_0}\)} (b-2-1)
            (b-1-2) edge node[auto]{\(\scriptstyle{\phi_1}\)} (b-2-2) edge node[auto]{\(\scriptstyle{g_1}\)} (b-1-3)
            (b-1-3) edge node[auto]{\(\scriptstyle{\phi_i}\)} (b-2-3) edge (b-1-4)
            (b-2-1) edge[-,double] (b-2-2)
            (b-2-2) edge[-,double] (b-2-3)
            (b-2-3) edge[-,double] (b-2-4)
            (b-1-4) edge[-,double] (b-2-4);
        \end{tikzpicture}.
    \end{center}

When I render it, I end up with weird slanted lines going into the dots.

slanted arrows

The problem is even more extreme if I just use \dots. How can I fix this to make the lines straight? I'm aware that tikz-cd probably fixes this, but I don't have time at the moment to learn the syntax and convert everything, so I'm still doing things the old way for now.

  • 1
    Add anchor=center: \matrix (b) [matrix of math nodes, row sep=2em, column sep=2em, nodes={anchor=center}] .... . –  May 03 '19 at 22:43
  • 1
    Hi Harry. For future reference, even without learning the syntax of tikz-cd then its asymmetric rectangle node shape is really useful for this. See https://tex.stackexchange.com/a/3894/86 for an example. – Andrew Stacey May 03 '19 at 22:57
  • @LoopSpace Great, thanks! – Harry Gindi May 03 '19 at 22:58

2 Answers2

2

Use tikz-cd for this job.

The syntax is much simpler and the arrow labels are typeset correctly in script style.

\documentclass{article}
\usepackage{amsmath}
\usepackage{tikz-cd}

\begin{document}

\begin{tikzcd}
D(0) \arrow[r,"g_0"] \arrow[d,"\phi_0"] &
D(1) \arrow[r,"g_1"] \arrow[d,"\phi_1"] &
\cdots \arrow[r] \arrow[d,"\phi_i"] &
C \arrow[d,equal]
\\
C \arrow[r,equal] & C \arrow[r,equal] & \cdots \arrow[r,equal] & C
\end{tikzcd}

\end{document}

enter image description here

You can use phantoms to make the arrow straight. Note that I removed the wrong font=\scriptsize declaration.

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

\begin{document}

\begin{tikzpicture}
\matrix (b) [matrix of math nodes, row sep=2em, column sep=2em]
  {
   D(0) & D(1) & \cdots\vphantom{()} & C\vphantom{()} \\
   C & C & \cdots\vphantom{C} & C \\
  };
  \path[->]
  (b-1-1) edge node[auto]{\(\scriptstyle{g_0}\)} (b-1-2) edge node[auto]{\(\scriptstyle{\phi_0}\)} (b-2-1)
  (b-1-2) edge node[auto]{\(\scriptstyle{\phi_1}\)} (b-2-2) edge node[auto]{\(\scriptstyle{g_1}\)} (b-1-3)
  (b-1-3) edge node[auto]{\(\scriptstyle{\phi_i}\)} (b-2-3) edge (b-1-4)
  (b-2-1) edge[-,double] (b-2-2)
  (b-2-2) edge[-,double] (b-2-3)
  (b-2-3) edge[-,double] (b-2-4)
  (b-1-4) edge[-,double] (b-2-4);
\end{tikzpicture}

\end{document}

Old style

egreg
  • 1,121,712
  • I'm aware tikz-cd is better for this, but I have like 100 diagrams in the older pre-tikz-cd style, and I'm not ready to fix it yet. If you're aware of a way to quickly fix a whole bunch of these at once, I'm all ears. – Harry Gindi May 03 '19 at 23:17
  • @HarryGindi Sorry, you used the wrong tool. I'll try a fix for the other style, but the typesetting of labels is really awful. – egreg May 03 '19 at 23:18
  • 1
    @HarryGindi Here it is. But conversion is quite easy, in my opinion. – egreg May 03 '19 at 23:24
0

A simple fix to your problem is to add the following line just below \begin{tikzpicture}:

 \tikzset{every node/.style={anchor=center, text centered}} 

This gives me (I use TeXstudio + XeLaTeX, but it shouldn't make a difference):

Diagram with nodes properly aligned