5

I'd like to draw just part of a path. In my particular case I want to hide the rightmost part of a brace path in part of a larger picture. My current solution is to draw a rectangle over it, but that's really hacky. Is there a better way?

\documentclass[tikz,border=2pt]{standalone}
\usepackage{tikz}
\usetikzlibrary{decorations.pathreplacing}
\begin{document}
\begin{tikzpicture}
  \draw[decorate,decoration={brace}] (0,0) -- (5,0);
  \fill[white] (4,-1) rectangle (6,1);
\end{tikzpicture}
\end{document}

This produces the desired result (note the lack of the curl on the right of the brace): Example

2 Answers2

8

If you really want to hide part of the bracket - as opposed to drawing an unbalanced, but complete, bracket - then clipping is probably easiest. For your MWE, this is quite simple. For example:

\documentclass[tikz,border=2pt]{standalone}
\usetikzlibrary{decorations.pathreplacing}
\begin{document}
\begin{tikzpicture}
  \path [clip] (0,-.5) rectangle (4,.5);
  \draw[decorate,decoration={brace}] (0,0) -- (5,0);
\end{tikzpicture}
\end{document}

clipped bracket

Note that the clipped area specifies the area to be shown - nothing outside that area will be drawn.

Clipping affects everything in the scope of the clipping command. This may not be what you want...

Suppose you want to reproduce the following:

target

You might try this:

\documentclass[tikz,border=2pt]{standalone}
\usetikzlibrary{decorations.pathreplacing}
\begin{document}
\begin{tikzpicture}
  \path [clip] (0,-.5) rectangle (4,.5);
  \draw[decorate,decoration={brace}] (0,0) -- (5,0);
  \path [draw, fill] foreach \i in {0,.5,1,...,5} {(\i,-.25) circle (.5pt)};
\end{tikzpicture}
\end{document}

but this code will produce:

too much hidden stuff

which is not what you wanted.

To limit the effect of the clipping, you can use a scope to specify just the stuff to which it should be applied:

\documentclass[tikz,border=2pt]{standalone}
\usetikzlibrary{decorations.pathreplacing}
\begin{document}
\begin{tikzpicture}
  \begin{scope}
    \path [clip] (0,-.5) rectangle (4,.5);
    \draw[decorate,decoration={brace}] (0,0) -- (5,0);
  \end{scope}
  \path [draw, fill] foreach \i in {0,.5,1,...,5} {(\i,-.25) circle (.5pt)};
\end{tikzpicture}
\end{document}

which produces the wanted result:

just right

cfr
  • 198,882
5

Two solutions:

  • clip: The drawing area is limited to the clipping region.
  • aspect: The nose/tip of the brace can be moved by option aspect. The advantage is, that the right side is not cut, but properly ended.

Example file with overprinting with white, clipping, and changed aspect ratio (default is 0.5):

\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{decorations.pathreplacing}
\begin{document}
\begin{tikzpicture}
  \draw[decorate,decoration={brace}] (0,0) -- (5,0);
  \fill[white, overlay] (4,-.2) rectangle (6,1);
\end{tikzpicture}

\begin{tikzpicture}
  \begin{scope}
    \clip[overlay] (-1, -1) rectangle (4, 1);
    \draw[decorate,decoration={brace}] (0,0) -- (5,0);
  \end{scope}
\end{tikzpicture}

\begin{tikzpicture}
  \draw[decorate,decoration={brace, aspect=0.625}] (0,0) -- (4,0);
\end{tikzpicture}
\end{document}

Result

Option overlay reduce the calculated bounding box, because the white rectangle or the clip region should not contribute to it. However the invisible brace parts do count. Depending on the location of the braces in the larger image, the overall bounding box might need corrections.

Example, which visualizes the bounding boxes:

\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{decorations.pathreplacing}
\setlength{\fboxsep}{0pt}
\setlength{\fboxrule}{.1pt}
\begin{document}
\fbox{\begin{tikzpicture}
  \draw[decorate,decoration={brace}] (0,0) -- (5,0);
  \fill[white,overlay] (4,-.2) rectangle (6,1);
\end{tikzpicture}}

\fbox{\begin{tikzpicture}
  \begin{scope}
    \clip[overlay] (-1, -1) rectangle (4, 1);
    \draw[overlay=false, decorate,decoration={brace}] (0,0) -- (5,0);
  \end{scope}
\end{tikzpicture}}

\fbox{\begin{tikzpicture}
  \draw[decorate,decoration={brace, aspect=0.625}] (0,0) -- (4,0);
\end{tikzpicture}}
\end{document}

Result

Heiko Oberdiek
  • 271,626