3

I have the following figure. It appears to me that the path labelled G messes up the centering of the figure in the document, probably because TikZ uses some invisible control points to draw this arc. (I've let TikZ show the bounding box of the picture to make the problem more evident.) How can I make the actual picture centered?

\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{fit}
\begin{document}
    \begin{figure}
    \centering
        \begin{tikzpicture}[node distance = 2cm, auto]
            \begin{scope}[local bounding box=a]
                \node (Kinf) {$K_\infty$};
                \node[above of=Kinf, right of=Kinf] (Linf) {$L_\infty$};
                \node[below of=Kinf, right of=Kinf, node distance=4cm] (K0) {$K_{0}$};
                \node[above of=K0, right of=K0] (L0)   {$L_{0}$};
                \draw[-] (Kinf) to node [] {$Z_\infty $} (Linf);
                \draw[-] (Kinf) to node [rotate=-45, midway, above] {$\ldots$} (K0);
                \draw[-] (Linf) to node [rotate=-45, midway, above] {$\ldots$} (L0);
                \draw[-] (K0) to node [] {$Z_0$} (L0);
                \draw (Kinf) to[out=-90, in=180, edge node={node [near start, left] {$\Gamma$}}] (K0);
                \draw[-] (Linf) to[out=0, in=0, looseness=2, edge node={node [near start] {$G$}}] (K0);
            \end {scope}
            \node [fit=(a),inner sep=0pt,draw] {};
        \end{tikzpicture}
    \end{figure}
\end{document}

1
  • Please give a fully compilable code. Commented Feb 21, 2020 at 11:56

2 Answers 2

2

With use of the bbox package (see TikZ & PGF manual, v 3.1.5b, 46 Bounding Boxes for Bézier Curves, page 581):

\documentclass[tikz, margin=3mm]{standalone}
\usetikzlibrary{bbox,
                fit}

\begin{document}
    \begin{tikzpicture}[
bezier bounding box=true,  % <---
node distance = 2cm, auto]
        \begin{scope}[local bounding box=a]
            \node (Kinf) {$K_\infty$};
            \node[above of=Kinf, right of=Kinf] (Linf) {$L_\infty$};
            \node[below of=Kinf, right of=Kinf, node distance=4cm] (K0) {$K_{0}$};
            \node[above of=K0, right of=K0] (L0)   {$L_{0}$};
            \draw[-] (Kinf) to node [] {$Z_\infty $} (Linf);
            \draw[-] (Kinf) to node [rotate=-45, midway, above] {$\ldots$} (K0);
            \draw[-] (Linf) to node [rotate=-45, midway, above] {$\ldots$} (L0);
            \draw[-] (K0) to node [] {$Z_0$} (L0);
            \draw (Kinf) to[out=-90, in=180, edge node={node [near start, left] {$\Gamma$}}] (K0);
            \draw[-] (Linf) to[out=0, in=0, looseness=2, edge node={node [near start] {$G$}}] (K0);
        \end {scope}
        \node [fit=(a),inner sep=0pt,draw] {};
    \end{tikzpicture}
\end{document}

enter image description here

Addendum & edit: slightly different design. In nodes' positioning are used the positioning package and syntax, for edge labels are used the quotes library. Also it works without bbox library and code is a bit shortened:

\documentclass[tikz, margin=3mm]{standalone}
\usetikzlibrary{fit,
                positioning,
                quotes}

\begin{document}
    \begin{tikzpicture}[
      node distance = 12mm and 11mm,    % <---
                       ]
\begin{scope}[local bounding box=a]     % in real document is not needed
    \node (Kinf)                {$K_\infty$};
    \node[above right=of Kinf] (Linf)                   {$L_\infty$};
    \node[below right=24mm and 11 mm of Kinf]   (K0)    {$K_{0}$};
    \node[above right=of K0]   (L0)   {$L_{0}$};
    \draw   (Kinf) to ["$Z_\infty $"]           (Linf) 
            (Kinf) to [sloped, "$\ldots$"]      (K0) 
            (Linf) to [sloped, "$\ldots$"]      (L0)
            (K0)   to ["$Z_0$"]                 (L0)
    (Kinf) to[out=240, in=180, "$\Gamma$" ']    (K0)
    (Linf) to[out=345, in=0,   
              looseness=1.5,  "$G$"]    (K0);
\end{scope}                             
\node[fit=(a),inner sep=0pt,draw=teal] {};  % in real document is not needed
    \end{tikzpicture}
\end{document}

enter image description here

*Warning: The first solution will not work with new version of TikZ, i.e. with version 3.1.6, because as stated in github.com/pgf-tikz/pgf/releases/tag/3.1.6:

The bbox library introduced in PGF 3.1.5 was removed because the author decided to impose restrictions on redistribution which violates freedoms 2 and 3 of the Free Software Definition, making it effectively non-free which is in contradiction with the GPLv2 + LPPLv1.3c terms that PGF/TikZ is distributed under. I further recommend that if there are files containing bbox code left over from a previous version that these are removed to avoid potential legal issues.

Note: the second answer in addendum not use bbox library, so it will works in new version of TikZ/PGF package too. See @Torbjørn T. comments below answer (thank you very much for info).

3
  • Note that the bbox library was removed from TikZ with version 3.1.6 due to licensing issues: github.com/pgf-tikz/pgf/releases/tag/3.1.6 Commented Sep 28, 2020 at 18:26
  • @TorbjørnT., hank you for info. I still have version 3.1.5b (which is now available). When new version will be available? What to do now? Add a note to the answer, that use of answer is on risk of user? Or that it will not work anymore? Or just delete answer? Commented Sep 28, 2020 at 18:53
  • It's available on GitHub, but I don't know when it will make its way to CTAN. The code will still work for those with 3.1.5, so perhaps just add a note that it will stop working if/when one upgrades to 3.1.6. In your second code it isn't that much of a problem though, so you could remove bbox from that. Commented Sep 28, 2020 at 19:01
1

The bbox library allows you to calculate the smallest bounding box that contains the figure with the option bezier bounding box=true.

Instead of using the fit library to frame this figure, I used the current bounding box dimensions. And since this box is too narrow on the right side (it is tangent to the frame), I made it slightly larger.

screenshot

\documentclass{article}
\usepackage{tikz}
%\usetikzlibrary {fit}
\usetikzlibrary{bbox}
\begin{document}
\begin{figure}
\centering
    \begin{tikzpicture}[node distance = 2cm, auto,bezier bounding box=true]
        \begin{scope}%[local bounding box=a]
            \node (Kinf) {$K_\infty$};
            \node[above of=Kinf, right of=Kinf] (Linf) {$L_\infty$};
            \node[below of=Kinf, right of=Kinf, node distance=4cm] (K0) {$K_{0}$};
            \node[above of=K0, right of=K0] (L0)   {$L_{0}$};
            \draw[-] (Kinf) to node [] {$Z_\infty $} (Linf);
            \draw[-] (Kinf) to node [rotate=-45, midway, above] {$\ldots$} (K0);
            \draw[-] (Linf) to node [rotate=-45, midway, above] {$\ldots$} (L0);
            \draw[-] (K0) to node [] {$Z_0$} (L0);
            \draw (Kinf) to[out=-90, in=180, edge node={node [near start, left] {$\Gamma$}}] (K0);
            \draw[-] (Linf) to[out=0, in=0, looseness=2, edge node={node [near start] {$G$}}] (K0);
        \end {scope}
%        \node [fit=(a),inner sep=0pt,draw] {};
        \draw (current bounding box.south west) rectangle
([xshift=5pt]current bounding box.north east);
    \end{tikzpicture}
\end{figure}
\end{document}
4
  • 2
    @Schrödinger'scat The bbox library is written anonymously, and in any case people can use open-source code however they wish Commented Feb 21, 2020 at 17:11
  • @JosephWright This is true but "The bbox library makes it possible to disregard control points in the calculation" is a misleading statement. This is not an appropriate description of what the library does. The library establishes the correct bounding box, which is not achieved by (just) disregarding the control points. Commented Feb 21, 2020 at 17:19
  • 1
    @Schrödinger'scat I updated my answer, The bbox library allows you to calculate the smallest bounding box that contains the figure Commented Feb 21, 2020 at 17:40
  • I feel the very least you could do is to acknowledge in the answer that you got told by others that the answer was not entirely correct. The cleaner way IMHO would be to remove the post altogether, an earlier answer already pointed out that the bbox library can be used here. Commented Feb 21, 2020 at 17:42

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.