[Tex/LaTex] Adjusting the width of a displaymath environment

equationsmarginsspacingtikz-cd

I want to adjust the width of an displaymath environment whose contents (a tikzcd diagram rather than an actual equation) are too wide to fit in the margins. The obvious thing to do is to use the adjustwidth environment, but this doesn't work as well as I would like: the vertical spacing of the result is sometimes quite ugly. (It seems to be some kind of conflict with mathtools, see below.)

It seems to me that the problem is in the implementation of adjustwidth using list. Perhaps there is an alternative / more direct way of adjusting the margins of part of a page that works better with equation?

enter image description here

\documentclass[12pt,a4paper]{article}

\usepackage{mathtools}
\usepackage{changepage}
\usepackage{tikz-cd}

\newcommand{\sample}{Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate.}

\begin{document}
\sample
\begin{adjustwidth}{-0.5in}{-0.5in}
\[
\begin{tikzcd}
\bullet \dar \rar &
\bullet \dar \rar &
\bullet \dar \rar &
\bullet \dar \rar &
\bullet \dar \rar &
\bullet \dar \rar &
\bullet \dar \rar &
\bullet \dar \rar &
\bullet \dar \rar &
\bullet \dar \\
\bullet \rar &
\bullet \rar &
\bullet \rar &
\bullet \rar &
\bullet \rar &
\bullet \rar &
\bullet \rar &
\bullet \rar &
\bullet \rar &
\bullet
\end{tikzcd}
\]
\end{adjustwidth}
\sample
\end{document}

enter image description here

Best Answer

The first thing to try is, of course, reducing the arrow lengths. If all else fails, use \mathclap that, however, requires ampersand replacement; or enclose the diagram in an lrbox.

I'll show all three possibilities.

\documentclass[12pt,a4paper]{article}

\usepackage{mathtools}
\usepackage{tikz-cd}

\newsavebox{\wideeqbox}

\newcommand{\sample}{Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod 
tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud 
exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor 
in reprehenderit in voluptate.}

\begin{document}
\sample
\[
\mathclap{
\begin{tikzcd}[ampersand replacement=\&]
\bullet \dar \rar \&
\bullet \dar \rar \&
\bullet \dar \rar \&
\bullet \dar \rar \&
\bullet \dar \rar \&
\bullet \dar \rar \&
\bullet \dar \rar \&
\bullet \dar \rar \&
\bullet \dar \rar \&
\bullet \dar \\
\bullet \rar \&
\bullet \rar \&
\bullet \rar \&
\bullet \rar \&
\bullet \rar \&
\bullet \rar \&
\bullet \rar \&
\bullet \rar \&
\bullet \rar \&
\bullet
\end{tikzcd}
}
\]
\sample
\[
\begin{lrbox}{\wideeqbox}
$\begin{tikzcd}
\bullet \dar \rar &
\bullet \dar \rar &
\bullet \dar \rar &
\bullet \dar \rar &
\bullet \dar \rar &
\bullet \dar \rar &
\bullet \dar \rar &
\bullet \dar \rar &
\bullet \dar \rar &
\bullet \dar \\
\bullet \rar &
\bullet \rar &
\bullet \rar &
\bullet \rar &
\bullet \rar &
\bullet \rar &
\bullet \rar &
\bullet \rar &
\bullet \rar &
\bullet
\end{tikzcd}$
\end{lrbox}
\makebox[0pt]{\usebox{\wideeqbox}}
\]
\sample
\[
\begin{tikzcd}[column sep=2em,row sep=1.5em]
\bullet \dar \rar &
\bullet \dar \rar &
\bullet \dar \rar &
\bullet \dar \rar &
\bullet \dar \rar &
\bullet \dar \rar &
\bullet \dar \rar &
\bullet \dar \rar &
\bullet \dar \rar &
\bullet \dar \\
\bullet \rar &
\bullet \rar &
\bullet \rar &
\bullet \rar &
\bullet \rar &
\bullet \rar &
\bullet \rar &
\bullet \rar &
\bullet \rar &
\bullet
\end{tikzcd}
\]
\sample

\end{document}

enter image description here

The lrbox solution can be packaged into an environment:

\newsavebox{\wideeqbox}
\newenvironment{wideeq}
  {\begin{displaymath}\begin{lrbox}{\wideeqbox}$\displaystyle}
  {$\end{lrbox}\makebox[0pt]{\usebox{\wideeqbox}}\end{displaymath}}

Then

text
\begin{wideeq}
\begin{tikzcd}
...
\end{tikzcd}
\end{wideeq}
text

will do. Any math mode material can be used.