2

I have a wide commutative diagram that I want to be centered on the page, but instead begins at the left margin and goes too far to the right. How can I make the diagram be in the center of the page?

I have tried to scale the size down, but when the diagram was correctly centered the text was too small. I have reduced the column separation as much as I can and I have also tried using the center environment, to no avail. Thanks in advance!

Here is a the diagram:

\documentclass{report}

\usepackage[english]{babel}
\usepackage{amsmath, amssymb, tikz-cd, blindtext}

\newcommand{\C}{\mathbb{C}}

\begin{document}

\blindtext
\[
\begin{tikzcd}[column sep = 1em]
    K^0_{S^1}(E_0 \times \C, p_1^*P_k) 
        \rar \dar
    & K_{S^1}^0(U \times \C) \oplus K^0_{S^1}(V \times \C)
        \rar \dar
    & K_{S^1}^0(U\cap V \times \C) 
        \rar \dar
    & K^1_{S^1}(E_0 \times \C, p_1^*P_k)
        \dar
    \\
    K^0_{S^1}(E_0, P_k) 
        \rar
    & K_{S^1}^0(U) \oplus K^0_{S^1}(V)
        \rar
    & K_{S^1}^0(U \cap V) 
        \rar
    & K^1_{S^1}(E_0, P_k)
\end{tikzcd}
\]
\blindtext

\end{document}

Wide commutative diagram shifted to the right

5
  • 2
    You could use the chngpage package and wrap the thing (including \[...\]) in ` \begin{adjustwidth}{-3cm}{-3cm}...\end{adjustwidth}`
    – daleif
    Commented Oct 26, 2022 at 7:31
  • Thanks, this solves it. The adjustpage for some reason added some unnecessary extra vertical space before the diagram, so I had to add \vspace{-\baselineskip} to fix this.
    – Motmot
    Commented Oct 26, 2022 at 8:32
  • 1
    It did not in my test, it even seems smaller, so you might want to add that example to your question (don't change the rest)
    – daleif
    Commented Oct 26, 2022 at 8:40
  • 1
    The adjustwith effect can be done with TikZ itself: trim left={([xshift=3cm]current bounding box.west)}, trim right={([xshift=-3cm]current bounding box.east)}. Commented Oct 26, 2022 at 9:49
  • @daleif I added in the adjustwidth to my main document, not the test code I wrote for the question. Probably something in there that is affecting the spacing. Either way it's not a problem, vertical spacing is easy to fix. Thanks for your suggestion.
    – Motmot
    Commented Oct 26, 2022 at 10:03

1 Answer 1

4

Centering a tikz-cd

You can employ a TikZ function to center the whole picture to the text (not the page when different margins are used).

These are trim left and trim right. Setting both to the same coordinate means that the picture will have no horizontal width and since \[ … \] will center its content on its own it will then center the picture on that coordinate. If that coordinate is set to the center of the whole picture then the whole picture will be centered around its center.

If you need this often, it be wise to define your own style for it which is why I defined a center picture style for that.

(The \[ … \] is not necessary for a tikz-cd diagram since it will be using math-mode for the cells anyway, instead of \[ … \] the center environment might be used but the vertical spacing might be different.)

Or a smaller picture

I'm no expert on commutative diagrams and what the rules are but I've added two separte examples:

  1. The first one (with down and trim for the nodes of the first row) aligns the first row in a staircase.
  2. The second one (with downup and trim) only alternates the vertical placement.

Both trim the bounding boxes of the nodes by 1cm on both sides.

I've chosen 1.5\baselineskip randomly, it might be wiser to set this value in relation to the diagram's setup (vertical spacing and sizes of nodes).

If you're using formulas inside the cells that are very high or low much more manual control will be needed.


Here, all nodes are still in the same \matrix row but are set at a different height.

For more complex diagrams it might be wise to just distribute these formulas on different nodes, either again with trim node or a negative column seperation.

Code

\documentclass{report}
\usepackage[english]{babel}
\usepackage{amsmath, amssymb, tikz-cd, blindtext}
\newcommand{\C}{\mathbb{C}}
\tikzset{
  trim node/.default=1cm,
  trim node/.style={
    overlay,
    append after command={% restore smaller bounding box
      ([xshift={+#1}]\tikzlastnode.north west)
      ([xshift={+-#1}]\tikzlastnode.south east)}},
  down and trim/.default=1cm,
  down and trim/.style={
    yshift=-(\pgfmatrixcurrentcolumn-1)*1.5\baselineskip,
    trim node={#1}},
  downup and trim/.default=1cm,
  downup and trim/.style={
    yshift=iseven(\pgfmatrixcurrentcolumn) ? -1.5\baselineskip : 0pt,
    trim node={#1}},
  -|/.style={to path={-|(\tikztotarget)\tikztonodes}},
  |-/.style={to path={|-(\tikztotarget)\tikztonodes}},
  -| sl/.style={-|, xslant=-1},
  |- sl/.style={|-, xslant= 1},
  center picture/.style={
    trim left=(current bounding box.center),
    trim right=(current bounding box.center)}}
\begin{document}

\blindtext
\[
\begin{tikzcd}[column sep = 1em, center picture]
      K^0_{S^1}(E_0 \times \C, p_1^*P_k)                   \rar \dar
    & K_{S^1}^0(U \times \C) \oplus K^0_{S^1}(V \times \C) \rar \dar
    & K_{S^1}^0(U\cap V \times \C)                         \rar \dar
    & K^1_{S^1}(E_0 \times \C, p_1^*P_k)                        \dar \\
      K^0_{S^1}(E_0, P_k)                                  \rar
    & K_{S^1}^0(U) \oplus K^0_{S^1}(V)                     \rar
    & K_{S^1}^0(U \cap V)                                  \rar
    & K^1_{S^1}(E_0, P_k)
\end{tikzcd}
\]
\blindtext
\[
\begin{tikzcd}[column sep = 1em, /tikz/row 1/.append style={nodes=down and trim}]
      K^0_{S^1}(E_0 \times \C, p_1^*P_k)                   \rar[-|] \dar
    & K_{S^1}^0(U \times \C) \oplus K^0_{S^1}(V \times \C) \rar[-|] \dar
    & K_{S^1}^0(U\cap V \times \C)                         \rar[-|] \dar
    & K^1_{S^1}(E_0 \times \C, p_1^*P_k)                            \dar \\
      K^0_{S^1}(E_0, P_k)                                  \rar
    & K_{S^1}^0(U) \oplus K^0_{S^1}(V)                     \rar
    & K_{S^1}^0(U \cap V)                                  \rar
    & K^1_{S^1}(E_0, P_k)
\end{tikzcd}
\]
\blindtext
\[
\begin{tikzcd}[column sep = 1em, /tikz/row 1/.append style={nodes=downup and trim}]
      K^0_{S^1}(E_0 \times \C, p_1^*P_k)                   \rar[-| sl] \dar
    & K_{S^1}^0(U \times \C) \oplus K^0_{S^1}(V \times \C) \rar[|- sl] \dar
    & K_{S^1}^0(U\cap V \times \C)                         \rar[-| sl] \dar
    & K^1_{S^1}(E_0 \times \C, p_1^*P_k)                               \dar \\
      K^0_{S^1}(E_0, P_k)                                  \rar
    & K_{S^1}^0(U) \oplus K^0_{S^1}(V)                     \rar
    & K_{S^1}^0(U \cap V)                                  \rar
    & K^1_{S^1}(E_0, P_k)
\end{tikzcd}
\]
\blindtext
\end{document}

Output

enter image description here

2
  • rounded corners Commented Oct 26, 2022 at 9:45
  • Thanks, these are really cool solutions! I hadn't even thought of these options for making the diagram less wide. I really like the second one in particular.
    – Motmot
    Commented Oct 26, 2022 at 10:01

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .