# Centering diagrams made with tikz-cd

I used tikzcd in equation environment so that the diagram should be placed on a new line and centered but I don't know why it is not perfectly centered. Here is my code:

\documentclass{article}
\usepackage{tikz}
\usepackage{tikz-cd}
\usetikzlibrary{matrix,arrows}
\begin{document}
The diagram is not perfectly centered:
$\begin{tikzcd} H^0(U\cup V) \rar & H^0(U)\oplus H^0(V) \rar & H^0(U\cap V) \ar[out=-20, in=160]{dll} \\ H^1(U\cup V) \rar & H^1(U)\oplus H^1(V)\rar & H^1(U\cap V) \ar[out=-20, in=160]{dll} \\ H^2(U\cup V) \rar & H^2(U)\oplus H^2(V)\rar & H^2(U\cap V) \end{tikzcd}$
\end{document}


The diagram appears slightly on the right side of the page. Does anyone know how to fix this problem?

• Put this in the preamble. \usepackage[margin=1in]{geometry} This will make all margins equal 1 in, resulting in a centered diagram. Now you need to read the manual for 'geometry" at texdoc.net/texmf-dist/doc/latex/geometry/geometry.pdf. In this well written documentation you will learn about the page layout and discover that geometry provide you will many useful options to control the layout. – R. Schumacher Dec 17 '16 at 17:56
• @R.Schumacher The horizontal margins are symmetric in a standard article, making them narrower might by accident solve the problem, but fixing the actual cause (see my answer) is certainly better. – Torbjørn T. Dec 17 '16 at 18:04
• @Torbjorn +1 Great answer and I learned something I had missed about Tikz diagrams – R. Schumacher Dec 17 '16 at 19:44

The reason for this is that the curved arrows define helper coordinates that are placed far outside the drawn parts of the diagram, so the diagram contains a lot of whitespace, and its width is actually wider than the width of the text block. Look in the log and you'll see a warning about an Overfull \hbox (72.53682pt too wide). To visualize this, add the backgrounds TikZ library, and add [show background rectangle] to the options of the tikzcd environment. (See code below.)

To fix it, add overlay to the options of the \ar commands that draws the arrow. This option means that those commands will not be taken into account when the bounding box of the diagram is defined.

\documentclass{article}
\usepackage{tikz-cd}
\usetikzlibrary{matrix,arrows,backgrounds}
\begin{document}
The diagram is not perfectly centered:
$\begin{tikzcd}[show background rectangle] H^0(U\cup V) \rar & H^0(U)\oplus H^0(V) \rar & H^0(U\cap V) \ar[out=-20, in=160]{dll} \\ H^1(U\cup V) \rar & H^1(U)\oplus H^1(V)\rar & H^1(U\cap V) \ar[out=-20, in=160]{dll} \\ H^2(U\cup V) \rar & H^2(U)\oplus H^2(V)\rar & H^2(U\cap V) \end{tikzcd}$
The diagram is centered:
$\begin{tikzcd}[show background rectangle] H^0(U\cup V) \rar & H^0(U)\oplus H^0(V) \rar & H^0(U\cap V) \ar[overlay,out=-20, in=160]{dll} \\ H^1(U\cup V) \rar & H^1(U)\oplus H^1(V)\rar & H^1(U\cap V) \ar[overlay,out=-20, in=160]{dll} \\ H^2(U\cup V) \rar & H^2(U)\oplus H^2(V)\rar & H^2(U\cap V) \end{tikzcd}$
\end{document}


No such problem for a commutative diagram made with pstricks. Loading auto-pst-pdf enables you to compile with pdflatex, provided it is launched with the -shell-escapeswitch (TeX Live, MacTeX) or --enable-write18.

\documentclass{article}

\usepackage[showframe]{geometry}%
\usepackage{pst-node, auto-pst-pdf}%

\begin{document}

$\begin{psmatrix}[rowsep =5ex] H^0(U \cup V) & H^0(U) \oplus H^0(V) & H^0(U \cap V)\pnode[0,0.15]{S1}\\ \pnode[0,0.08]{S2}H^1(U \cup V) & H^1(U) \oplus H^1(V) & H^1(U \cap V)\pnode[0,0.15]{S3} \\ \pnode[0,0.08]{S4}H^2(U \cup V) & H^2(U) \oplus H^2(V) & H^2(U \cap V) \psset{arrows=->, linewidth=0.5pt, nodesep=4pt, arrowinset=0.12} \ncline{1,1}{1,2}\ncline{1,2}{1,3} \ncline{2,1}{2,2}\ncline{2,2}{2,3} \ncline{3,1}{3,2}\ncline{3,2}{3,3} \psset{angleA=-15,angleB=165, ncurv=0.8, nodesepB=2pt} \nccurve[angleA=-15,angleB=165, ncurv=0.8, nodesepB=2pt]{S1}{S2}%{1,3}{2,1} \nccurve{S3}{S4} \end{psmatrix}$

\end{document}