5

I have the following code

\documentclass{report}
\usepackage{tikz}
\usetikzlibrary{intersections}

\begin{document}
bla bla bla

\begin{tikzpicture}
\def\r{1}; % radius
\def\a{80}; % 1st angle
\def\b{170} % 2nd angle
\pgfmathsetmacro\px{\r/cos(\a)}
\pgfmathsetmacro\qy{\r/cos(\b-90)}

\coordinate (p) at (\px,0);
\coordinate (p1) at (2*\a:\px);
\coordinate (p2) at (-2*\a:\px);
\coordinate (q1) at (0,\qy);
\coordinate (q2) at (2*\b-90:\qy);
\path[name path=lineup] (p)-- (p1);
\path[name path=linedown] (p)-- (p2);
\path[name path=lineleft] (q1)-- (q2);

\draw[name intersections={of=lineup and lineleft,by={r1}}];
\draw[name intersections={of=linedown and lineleft,by={r2}}];

\draw (0,0) circle [radius=\r];
\draw (0,0) -- (\a:\r);
\draw (0,0) -- (-\a:\r);
\draw (0,0) -- (\b:\r);
\draw (p) -- (r1) -- (r2) -- cycle;
\end{tikzpicture}

bla bla bla
\end{document}

which gives me the following picture. pdf

As you can see there is a lot of empty space above and below the triangle. This comes from the fact that the left corners (r1 and r2 in the code) of the triangle are intersection of virtual line segments (lineup, linedown and lineleft in the code).

Question: How can I ask Tikz to ignore these virtual line segments when computing the size of the window? I tried to do something with \clip, but I was not successful.

Note that my picture depends on three parameters \r, \a and \b, which I define at the beginning of the tikzpicture. I don't want to have a clip with some arbitrary value which would correspond only to a specific choice of parameters.

4
  • 1
    Look up the key overlay and other options related to "bounding box". Jun 2 '16 at 13:19
  • 1
    Some examples with use as bounding box: tex.stackexchange.com/q/43621/1952
    – Ignasi
    Jun 2 '16 at 13:55
  • 2
    Thanks @Qrrbrbirlbel. \pgfresetboundingbox placed before \draw (p) -- (r1) -- (r2) -- cycle; makes it. You might want to post it as an answer in order to get the credit. Otherwise I'll write the answer myself later to not let an unanswered question. Jun 2 '16 at 13:57
  • Rereading the question: \clipping only clips what comes after it (in the current scope/group). Jun 2 '16 at 20:59
6

The coordinates p, p1, p2, q1 and q2 as well as the paths that use them are bigger than the stuff you actually draw. They are still considered by PGF to be part of the picture and thus the bounding box of the picture gets enlarged accordingly.

In your case, the easiest solution would be to put the whole section including the name intersections-auxiliary paths in a scope with the options overlay. This makes it so that the whole scope will not be considered to be part of the bounding box.

\begin{scope}[overlay]
  \coordinate (p) at (\px,0);
  \coordinate (p1) at (2*\a:\px);
  \coordinate (p2) at (-2*\a:\px);
  \coordinate (q1) at (0,\qy);
  \coordinate (q2) at (2*\b-90:\qy);
  \path[name path=lineup] (p)-- (p1);
  \path[name path=linedown] (p)-- (p2);
  \path[name path=lineleft] (q1)-- (q2);

  \path[name intersections={of=lineup and lineleft,by={r1}}];
  \path[name intersections={of=linedown and lineleft,by={r2}}];
\end{scope}

Even though the name intersections path do not declare coordinates outside of the desired bounding box and thus wouldn't need to be inside the scope, because the name path key works locally it is easier to include them. (There's also global name path.)


You could also use \pgfresetboundingbox (which does what is says) as you said but then you should make sure there's nothing before it that does protrude outside the final bounding box.

1
  • 1
    (+1) I didn't realise you could do it this way with overlay. Interesting.
    – cfr
    Jun 2 '16 at 20:55

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.