Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

With TikZ, is it possible to only clip horizontally or vertically?

Normally, when clipping, e.g.,

\documentclass{article}
\usepackage{tikz}
\begin{document}
\begin{tikz}
  \clip (-1,-1) rectangle (1,1);
  \draw (-2,0) -- (2,0);
\end{document}
\end{tikz}

a rectangular region is clipped and this whole region is included in the bounding box of the picture. When some picture elements' placement is not known in advance, e.g., in plots, this may mean that the bounding box is too large (or small).

One way to deal with this would be to clip only horizontally (e.g., plot range is known and we plot using a table with plotpoints outside that range, but not plot values) or vertically. But how could that be achieved?

share|improve this question
3  
have you tried what happens when you apply a clip rectangle with a huge width or height compared to the region to clip? This could simulate something like a vertical or horizontal clip as long as the clipping window does not affect the image bounding box itself – which I don't know and didn't test. –  Benedikt Bauer Dec 11 '13 at 12:55
1  
Please make your code compilable (if possible), or at least complete it with \documentclass{...}, \begin{document}, and \end{document}. That may seem tedious to you, but think of the extra work it represents for TeX.SX users willing to give you a hand. Help them help you: remove that one hurdle between you and a solution to your problem. –  Jubobs Dec 11 '13 at 12:55
1  
Although @BenediktBauer's suggestion is the first I would consider, I can think of one thing that might go wrong: the effect on the bounding box. Indeed, the real difficulty here would be to ensure that the bounding box was computed correctly. –  Loop Space Dec 11 '13 at 13:21
    
As mentioned in the question, @BenediktBauer's suggestion does not work: the bounding box encompasses the clipping path. –  equaeghe Dec 11 '13 at 13:31
1  
@Jubobs: MWE done. –  equaeghe Dec 11 '13 at 13:43

2 Answers 2

There are two issues here:

  1. Use \clip with a "huge" rectangle, while avoiding that clipping rectangle to be used as part of the BoundingBox. This can be solved by using \clip inside a pgfinterruptboundingbox environment.

  2. After the clipping area is set this way, we perform the drawing, but then the BoundingBox of the figure is updated for each stroke (even if it is not visible for strokes outside of the clipping area). So we have to reset the BoundingBox to the clipped area, after the figure is drawn. This can be done putting \pgfresetboundingbox, and then [use as bounding box] for an appropiate path.

The tricky part is to figure the "appropiate path" for the second problem. It has to be the intersection of the clipping area used in 1 with the real BoundingBox generated in 2.

Here is a (convoluted) possibility:

\documentclass{article}
\usepackage{tikz}
\newcommand\flower[1][black]{
    \foreach \a in {0,10,...,360} {
        \draw[#1] (\a:2) circle(1.5); 
    }
}
\begin{document}
A---%
\begin{tikzpicture}
    \coordinate (left) at (-1.3, -20);  % -20 is arbitrarily large
    \coordinate (right) at (1.3, 20);   % 20 is arbitrarily large
    \begin{pgfinterruptboundingbox}     % Clip, without affecting BB
        \clip (left) rectangle (right);
    \end{pgfinterruptboundingbox}
    \flower % This draws the "plot" (and creates a new BB, outside the clip)
    % Save new BB top and bottom
    \coordinate (top) at (current bounding box.north);
    \coordinate (bottom) at (current bounding box.south);
    % Clear BB
    \pgfresetboundingbox

    % Set a new BB adjusted to the clipped part
    \path[use as bounding box] (left|-bottom) rectangle (right|-top);
\end{tikzpicture}%
---B
\end{document}

Produces:

Result

share|improve this answer

I think it is possible by carefully selecting the dimensions of the rectangle. In your example, you should select a rectangle whose height is equal to the width of the line. In this case it is \pgflinewidth.

\documentclass{article}
\usepackage{tikz}
\begin{document}
\begin{tikzpicture}
  \clip (-1,-0.5\pgflinewidth) rectangle (1,0.5\pgflinewidth);
  \draw (-2,0) -- (2,0);
\end{tikzpicture}
\end{document}

enter image description here

share|improve this answer
    
This does not work when the vertical dimensions of the material to be included is not known, as with plots. –  equaeghe Dec 11 '13 at 14:31

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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