# Half of tikzcd diagram is missing

Half of a commutative diagram (tikz-cd) is missing in the output. The rendering tool is pdflatex. The problem occurs when:

1. The documentclass is standalone.
2. The tikz-cd diagram is embedded in a tikzpicture.

Example:

\documentclass{standalone}
\usepackage{tikz}
\usetikzlibrary{cd}
\begin{document}
\begin{tikzpicture}
\begin{tikzcd}
A \arrow{r}{a} \arrow{d}{b} & B \arrow{d}{c} \\
C \arrow{r}{d} & D
\end{tikzcd}
\end{tikzpicture}
\end{document}


Apparently embedding a tikzcd diagram inside a tikzpicture environment introduces an unexpected indent that the standalone documentclass does not account for.

This is a problem for us, since we're using a setup to support tikzpictures in a vBulletin math forum. It recognizes \begin{tikzpicture}...\end{tikzpicture} as the markers that define the picture, and replaces it with a .svgz picture. As a math forum we also want to support commutative diagrams from the cd tikzlibrary. Since that apparently requires a toplevel tikzcd environment that doesn't fit into the structure.

What is causing the unexpected indent? And more importantly, how can we ensure that the diagram is rendered properly? That is, how can we get rid of the unexpected indent while still embedding the commutative diagram in a tikzpicture environment?

I don't think that it is a good idea to use tikzcd inside tikzpicture because it is not in the spirit of tikz to have drawing "environments".

But if you really need it, you can define a new environment scopecd like this

\documentclass[tikz,border=7pt]{standalone}
\usetikzlibrary{cd}
% define new environment scopcd
\def\scopecd{\let\tikzpicture\scope\let\endtikzpicture\endscope\tikzcd}
\let\endscopecd\endtikzcd

\begin{document}
\begin{tikzpicture}
\fill[blue!14] circle(2);
\begin{scopecd}
K \arrow{r}{a} \arrow{d}{b} & B \arrow{d}{c} \\
C \arrow{r}{d} & D
\end{scopecd}
\end{tikzpicture}
\end{document}


Note : For me it is more natural to have in your forum different environments that can produce pictures ; tikzpicture, pgfpicture, tikzcd, ...

The tikzcd environment is already a tikzpicture, you don't have to nest them.

\documentclass{standalone}
\usepackage{tikz}
\usetikzlibrary{cd}
\begin{document}
\begin{tikzcd}
A \arrow{r}{a} \arrow{d}{b} & B \arrow{d}{c} \\
C \arrow{r}{d} & D
\end{tikzcd}
\end{document}


• nesting of tikzpicture (of any kind) usually lead to unexpected behavior (as you experienced). never do this! – Zarko Apr 7 '18 at 9:48
• @IlikeSerena Sorry, I don't completely understand, nesting tikzpicture may cause problems, this is one. If you are obliged to use tikzpicture, use a TikZ matrix of math nodes instead of a tikzcd environment. – CarLaTeX Apr 7 '18 at 9:49
• @IlikeSerena can't you just adapt your forum code to recognise tikzd and treat it like tikzpicture? – David Carlisle Apr 7 '18 at 9:56
• @IlikeSerena well yes of course just don't nest it. Given that currently you are explicitly nesting one inside the other then it's hard to avoid nesting other that just "don't do that" of course tex being tex you could redefine tikzpicture to check for tikcd and do nothing in that case but it's bound to break something and if your forum code is using tex syntax it's best to use valid tex syntax rather than redefine tex to fit – David Carlisle Apr 7 '18 at 10:04
• @IlikeSerena The tikzcd environment is a tikzpicture with a \matrix[matrix of math nodes]. – CarLaTeX Apr 7 '18 at 10:31