2

I want to vertically align a TikZ image inside angled brackets. I am not able to do it by adding yshift arguments to the knot environment or to the \draw or \strand commands.

\documentclass{article}

\usepackage{tikz} \usetikzlibrary{shapes, decorations, arrows.meta, decorations.markings, bending, knots, hobby, spath3, intersections}

\newcommand{\KP}[1]{% \begin{tikzpicture}[baseline=-\dimexpr\fontdimen22\textfont2\relax] #1 \end{tikzpicture}% } \newcommand{\KPthirtyoneone}{ \KP{ \begin{knot}[flip crossing=2, consider self intersections = true, clip width = 5, ignore endpoint intersections=false] \draw[scale=0.6, line width=0.6pt] (0,1) .. controls (0.5,0.8) .. (1,1); \strand[scale=0.6, line width=0.6pt, looseness=1.4] (0,0) to [out=0,in=0] (0.5,0.7) to [out=180,in=180] (1,0); \end{knot} } }

\begin{document}

[\Bigl\langle\hspace{-6pt}\vcenter{\hbox{\KPthirtyoneone}}\hspace{-6pt}\Bigr\rangle]

\end{document}

I have taken some of the code from https://tex.stackexchange.com/a/306010/128462. It produces the following output:

kpthirtyone

How do I vertically center the image inside the brackets?

JamesT
  • 3,169

2 Answers2

5

Like this?

enter image description here

(\alpha and \beta are added that baseline can be seen)

  • I assume that content of \KP can be any pictures with with different baseline positions, I suggest to add option for settings of tikzpicture baseline
  • I would remove \hspace{-6pt}\vcenter{\hbox{ at \hspace{-6pt}\vcenter{\hbox{ inserting in equation and rather use manual setting of \KPthirtyoneone baseline positioning:
\documentclass[margin=3mm, varwidth]{standalone}
%\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{arrows.meta, 
                bending, 
                decorations, decorations.markings, 
                hobby, 
                intersections, 
                knots, 
                shapes, spath3}
\newcommand{\KP}[2]{%
\begin{tikzpicture}[baseline=#1,
                    ]
    #2
\end{tikzpicture}%
                    }
\newcommand{\KPthirtyoneone}%
{
    \KP{1.4ex}{%
\begin{knot}[flip crossing=2, consider self intersections = true,
             clip width = 3, ignore endpoint intersections=false]
 \draw[scale=0.6, line width=0.6pt]
            (0,1) .. controls (0.5,0.8) .. (1,1);
 \strand[scale=0.6, line width=0.6pt, looseness=1.4]
            (0,0) to [out=0,in=0] (0.5,0.7) to [out=180,in=180] (1,0);
\end{knot}
        }
}
\begin{document}
\[
\alpha
\Bigl\langle
    \beta
    \KPthirtyoneone
\Bigr\rangle
\]
\end{document}

Zarko
  • 296,517
3

One of the problems here is that the bounding box of a tikzpicture might be larger than all of its contents, simply because of the way that TikZ computes bounding boxes - it goes for a "quick and easy" algorithm rather than a precise one. This is nothing to do with the knots library. What the knots library does which exacerbates this effect is that it mucks about with the curves quite a bit and this can all add to the bounding box. Normally this doesn't matter, but because your diagrams are very small then it is having an effect.

The simplest way to deal with this is to tell TikZ explicitly what the bounding box should be. Here, that's quite simple as there's an obvious containing rectangle. Once we've done that then TikZ ignores everything else as far as the bounding box is concerned and then you can use whatever method you like to centre the picture, whether that is via the baseline key or just the vcenter command as in your original code. The vcenter has the simplicity of not having to calculate things explicitly, whereas the baseline option offers you more control.

\documentclass{article}
%\url{https://tex.stackexchange.com/q/684222/86}
\usepackage{tikz}
\usetikzlibrary{
  shapes, 
%  decorations, % <-- loaded by decorations.markings
  arrows.meta, 
  decorations.markings, 
  bending, 
  knots, 
  hobby, 
%  spath3, % <-- loaded by knots
%  intersections % <-- loaded by knots
}

\newcommand{\KP}[1]{% \begin{tikzpicture}%[baseline=-\dimexpr\fontdimen22\textfont2\relax] #1 \end{tikzpicture}% } \newcommand{\KPthirtyoneone}{ \KP{ \useasboundingbox (0,0) rectangle (.6,.6); \begin{knot}[ flip crossing=2, consider self intersections = true, clip width = 5, ignore endpoint intersections=false, ] \draw[scale=0.6, line width=0.6pt] (0,1) .. controls (0.5,0.8) .. (1,1); \strand[scale=0.6, line width=0.6pt, looseness=1.4] (0,0) to [out=0,in=0] (0.5,0.7) to [out=180,in=180] (1,0); \end{knot} } }

\begin{document}

[\Bigl\langle\vcenter{\hbox{\KPthirtyoneone}}\Bigr\rangle]

\end{document}

Centred kauffman bracket

Andrew Stacey
  • 153,724
  • 43
  • 389
  • 751
  • Is there a way to tell TikZ to use the precise algorithm to calculate the bounding boxes instead of the quick and dirty one? – Apoorv Potnis Jun 11 '23 at 21:06
  • On a related note, thank you so much for your packages. They were of great use to me; it would have been impossible for me to learn the insides of tikz well enough and create the knot diagrams without these packages. – Apoorv Potnis Jun 11 '23 at 21:08
  • 1
    @ApoorvPotnis You're welcome! I make these packages to be useful to me, and publish them in the hope that they'll be useful to others, so it's always nice to hear that they are. – Andrew Stacey Jun 14 '23 at 21:35