# tikzcd - Spacing issues

I have created the following diagram (I just added a box around it):

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

\begin{document}
\fbox{
\begin{tikzcd}
\ar[loop left, in=150, out=-150, distance=2cm,"-1"']
\ar[loop left, in=148, out=-148, distance=3cm, "-2"']
\ar[loop left, in=146, out=-146, distance=3.7cm, font=\tiny, phantom,
"\dots"']
\ar[loop left, in=146, out=-146, distance=5cm, "-k"']
\ar[loop left, in=146, out=-146, distance=6cm, phantom, "\dots"'] m
\ar[loop above, in=60, out=120, distance=1cm,"\id_m"]
\ar[loop right, in=30, out=-30,distance=2cm,"+1"]
\ar[loop right, in=32, out=-32, distance=3cm, "+2"]
\ar[loop left, in=34, out=-34, distance=3.7cm, font=\tiny, phantom,
"\dots"]
\ar[loop right, in=34, out=-34, distance=5cm, "+k"]
\ar[loop left, in=34, out=-34, distance=6cm, font=\tiny, phantom,
"\dots"]
\end{tikzcd}}
\end{document}


The diagram is exactly as I wanted, but there is way too much space before and after. How to correct for that? Adding

 \vspace*{xxx}


is clearly a possibility, but I found it to be too much of a hack. Suggestions?

• Welcome to TeX.SX! You can help us to help you by providing the code for a small compilable document that shows your problem called MWE. Just edit your question and add missing code. - Do you mean (whitespace) above and below? – Bobyandbob Dec 26 '17 at 22:33

You can do

\begin{tikzcd}[
execute at begin picture={
\useasboundingbox (-4.5,-1.5) rectangle (4.5,1.5);
}
]


It does require a bit of trial and error to find appropriate coordinates though.

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

\begin{document}
\fbox{
\begin{tikzcd}[
execute at begin picture={
\useasboundingbox (-4.5,-1.5) rectangle (4.5,1.5);
}
]
\ar[loop left, in=150, out=-150, distance=2cm,"-1"']
\ar[loop left, in=148, out=-148, distance=3cm, "-2"']
\ar[loop left, in=146, out=-146, distance=3.7cm, font=\tiny, phantom,
"\dots"']
\ar[loop left, in=146, out=-146, distance=5cm, "-k"']
\ar[loop left, in=146, out=-146, distance=6cm, phantom, "\dots"'name=dot1] m
\ar[loop above, in=60, out=120, distance=1cm,"id_m"]
\ar[loop right, in=30, out=-30,distance=2cm,"+1"]
\ar[loop right, in=32, out=-32, distance=3cm, "+2"]
\ar[loop left, in=34, out=-34, distance=3.7cm, font=\tiny, phantom,
"\dots"]
\ar[loop right, in=34, out=-34, distance=5cm, "+k"]
\ar[loop left, in=34, out=-34, distance=6cm, font=\tiny, phantom,
"\dots"name=dot2]
\end{tikzcd}}
\end{document}


After messing around with your diagram I achieved this:

(blind text just to give a context).

The problem seems to be that the diagram has both height and depth too big, for some reason I don't understand.

# Edit:

The problem is, as @TorbjørnT. said, because \fbox includes the control points of the curve in the frame. So the solution below works only in case the diagram is symmetric! If the diagram is not symmetric, this solution will not work, unfortunately...

To work around the issue I used a modified version of a \putbox macro I had done a few weeks ago. The macro will draw a box around the diagram considering only its height, but not its depth.

Its not pretty, but I think it works:

\documentclass{article}
\usepackage{tikz}
\usepackage{blindtext}
\usetikzlibrary{cd}

\newcommand{\putbox}[2][]{%
\ifdefined\xboxh%
\relax%
\else%
\newlength{\xboxh}%
\newlength{\xboxw}%
\newlength{\xboxd}%
\fi%
\settoheight{\xboxh}{#2}%
\settowidth{\xboxw}{#2}%%
\settodepth{\xboxd}{#2}%
\frame{\phantom{\rule{\xboxw}{\dimexpr\xboxh}}}%
\if\relax\detokenize{#1}\relax%
\relax%
\else%
\setlength{\xboxh}{0.5\xboxw}%
\settowidth{\xboxw}{#1}%
\setlength{\xboxw}{0.5\xboxw}%
\hspace{\dimexpr-\xboxw-\xboxh}%
\vskip-\xboxh%
#1%
\hspace{\dimexpr \xboxh-\xboxw}%
\vskip\xboxd%
\vskip-\xboxh%
\fi\relax%
}

\newcommand{\mycd}{%
\begin{tikzcd}%
\ar[loop left, in=150, out=-150, distance=2cm,"-1"']%
\ar[loop left, in=148, out=-148, distance=3cm, "-2"']%
\ar[loop left, in=146, out=-146, distance=3.7cm, font=\tiny, phantom,%
"\dots"']%
\ar[loop left, in=146, out=-146, distance=5cm, "-k"']%
\ar[loop left, in=146, out=-146, distance=6cm, phantom, "\dots"'] m %
\ar[loop above, in=60, out=120, distance=1cm,"Id_m"]%
\ar[loop right, in=30, out=-30,distance=2cm,"+1"]%
\ar[loop right, in=32, out=-32, distance=3cm, "+2"]%
\ar[loop left, in=34, out=-34, distance=3.7cm, font=\tiny, phantom,%
"\dots"]%
\ar[loop right, in=34, out=-34, distance=5cm, "+k"]%
\ar[loop left, in=34, out=-34, distance=6cm, font=\tiny, phantom,%
"\dots"]%
\end{tikzcd}%
}

\begin{document}

\blindtext

\putbox[\mycd]{\mycd}

\blindtext

\end{document}

• The reason is that control points created by TikZ during the creation of the loops influence the bounding box. – Torbjørn T. Dec 26 '17 at 22:54
• @TorbjørnT. So the \fbox also captures the control points of the bezier curves? In this case my solution works by mere coincidence? – Phelype Oleinik Dec 26 '17 at 23:02
• The \fbox captures the area of the tikzpicture, and that area includes those control points. I don't know what your solution does exactly, so can't comment on coincidence. – Torbjørn T. Dec 26 '17 at 23:14
• Hahaha. Yes, one implies the other :P. And I thought I made something great... – Phelype Oleinik Dec 26 '17 at 23:17