[Tex/LaTex] Scaling tikz brace

decorationsscalingtikz-pgf

A tikz brace seems to be drawn in three stages (not necessarily in this order):

  • The "curls" at either end.
  • The "point" in the middle.
  • And a straight line connecting the "point" to either "curl" segment.

This makes it difficult to create short braces with the same amplitude as longer braces, because these three stages are too wide and end up overlapping each other:

\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{decorations.pathreplacing}

\begin{document}

\begin{tikzpicture}
\draw [decorate,decoration={brace,mirror,amplitude=10pt}]
    (0,0) -- (6,0) node [midway,yshift=-0.25in] {Text};
\end{tikzpicture}

\begin{tikzpicture} %% this fails; perhaps I need to xscale?
\draw [decorate,decoration={brace,mirror,amplitude=10pt}]
    (0,0) -- (0.5,0) node [midway,yshift=-0.25in] {Text};
\end{tikzpicture}

\begin{tikzpicture} %% but this is not as expected, either
\draw [xscale=1/12,decorate,decoration={brace,mirror,amplitude=10pt}]
    (0,0) -- (6,0) node [midway,yshift=-0.25in] {Text};
\end{tikzpicture}

\end{document}

enter image description here


Is it possible to correctly shorten this brace while keeping the amplitude and not scaling the text?

(I guess I'll also consider alternate decorations that won't have these errors at such small widths…)

Best Answer

Add this after decorations.pathreplacing is used. It will scale the brace if it is necessary.

\makeatletter
    \let\pgf@decorate@@brace@brace@code@old\pgf@decorate@@brace@brace@code
    \def\pgf@decorate@@brace@brace@code{
        \ifdim\pgfdecoratedremainingdistance<4\pgfdecorationsegmentamplitude
            \pgftransformxscale{\pgfdecoratedremainingdistance/4\pgfdecorationsegmentamplitude}
            \pgfdecoratedremainingdistance=4\pgfdecorationsegmentamplitude
        \fi
        \pgf@decorate@@brace@brace@code@old
    }
\makeatother

Playing code

\documentclass[tikz]{standalone}
\usetikzlibrary{decorations.pathreplacing}

\begin{document}

\makeatletter
    \let\pgf@decorate@@brace@brace@code@old\pgf@decorate@@brace@brace@code
    \def\pgf@decorate@@brace@brace@code{
        \ifdim\pgfdecoratedremainingdistance<4\pgfdecorationsegmentamplitude
            \pgftransformxscale{\pgfdecoratedremainingdistance/4\pgfdecorationsegmentamplitude}
            \pgfdecoratedremainingdistance=4\pgfdecorationsegmentamplitude
        \fi
        \pgf@decorate@@brace@brace@code@old
    }
\makeatother

\foreach\t in{10,20,...,360}{
    \tikz{
        \path(-1,-2)(3,1);
        \pgfmathsetmacro\x{1.1+sin(\t)}
        \pgfmathsetmacro\y{.1*cos(\t)}
        \pgfmathsetmacro\a{10+8*cos(\t)}
        \draw[decorate,decoration={brace,mirror,amplitude=\a pt}]
            (0,0)--
            node [yshift=-5-\a pt] {Text}
            (\x,\y);
    }
}
\end{document}
Related Question