1

I'm looking for a way to delete part of a previously drawn picture.

An easy way is drawing in the background color, but that does not work if the background is transparent.

Here's an example file.tex:

\documentclass[convert={density=100,outext=.png}]{standalone}
\usepackage{tikz}
\begin{document}
\begin{tikzpicture}
  % draw a black line   
  \draw (0,0) -- (2,1);
  % "delete" part of the line
  \draw[fill,color=white](0.5,0) rectangle (1.5,1);
\end{tikzpicture}
\end{document}

I use the standalone class to produce a single picture. When run with pdflatex -shell-escape file.tex, this builds both:

  • file.pdf
  • file.png

The pdf looks fine, since background is indeed white.

The png is built through the imagemagick convert tool and has a transparent background, and so the "deleted" part appears as a white rectangle.

I know I can use the -alpha remove -background white options for convert to turn the background white but I'd rather like the reverse: having transparent background, including the "deleting rectangle".

I thought about using a reverse clip (as in How can I invert a 'clip' selection within TikZ?) but I can't know in advance which part I don't want to be drawn, thus the need to delete rather than avoid drawing the unwanted part in the first place.

Is there any way to do that?

2
  • 1
    TikZ supports the transparency group of type knockout but not many PDF viewers do (neither does the unofficial online version of the manual I've linked to). You could also save the needed clip when you know it, write it to the aux file and then apply it on the second run. You could also place an empty rectangular box along a path with the mark connection node decoration where PGF/TikZ automatically draws to seperate lines. What's your actual usecase? Dec 3, 2022 at 16:32
  • Indeed, the knockout transparency group is exactly what I'm looking for! Too bad it's not well supported. The mupdf viewer does support it but imagemagick's convert tool (latest version 7.1.0-52) does not. My use case is the user using macros to describe sequential events, with some events affecting the drawing of previous events, thus the need to erase.
    – JeanPierre
    Dec 4, 2022 at 13:49

1 Answer 1

1

Taking the idea to save the path to the aux file so that it is available at the next run.

I didn't test it much but for this example it works. I might have experience with using the aux file but I'm no expert. Externalizing the picture with the external library won't work.


On any path of TikZ you can use the

  • remember path = <name> key so that it will be available at the next compilation and the
  • remembered path = <name> key that uses this path (if it is available).

The key will use \pgfpictureid as a prefix so that you can specify the same path name in different pictures.


I've also defined a reverse clip key that supports both a clockwise as well as a counter clockwise rectangle with maximum supported size. This is done on the PGF layer and with the quick commands for three reasons:

  1. they are quicker,
  2. they ignore transtormations and
  3. they don't update the bounding box (though, your \path[remember path=…] might use overlay anyway.

This is how you use it:

\begin{tikzpicture}
\fill[left color=blue, right color=green] (0,0) rectangle (2,1);
\clip[remembered path=box];
\draw (0,0) -- (2,1);
\path[remember path=box] (0.5, 0) rectangle (1.5, 1) [reverse clip];
\end{tikzpicture}

Code

\documentclass[border=5pt, tikz, convert]{standalone}
% Part 1: Quick PGF versions of reverse clips.
%% * uses quick commands that
%%   * ignore transformations and
%%   * don't contribute to the bounding box (we use overlay though anyway)
%% * only uses 16000pt since this is what PGF does, too (TeX'd allow 16384pt)
%% * clockwise and counterclockwise for nonzero and even odd rule
%%
\tikzset{% quick versions of reverse clips
  reverse clip/.is choice,
  reverse clip/clockwise/.code={%
    \pgfpathqmoveto{16000pt}{16000pt}%
    \pgfpathqlineto{16000pt}{-16000pt}%
    \pgfpathqlineto{-16000pt}{-16000pt}%
    \pgfpathqlineto{-16000pt}{16000pt}%
    \pgfpathclose},
  reverse clip/counter clockwise/.code={%
    \pgfpathqmoveto{16000pt}{16000pt}%
    \pgfpathqlineto{-16000pt}{16000pt}%
    \pgfpathqlineto{-16000pt}{-16000pt}%
    \pgfpathqlineto{16000pt}{-16000pt}%
    \pgfpathclose},
  reverse clip/.default=counter clockwise}

% Part 2: The actual keys
%         that save/use the path to/from the aux file.
%% * remember path will does what ever you do on the path
%%   and save it to the aux file
%% * remembered path will set the remembers path (if available)
%%   and use it as specified
%%
\makeatletter
\tikzset{
  remember path/.code={%
    \tikz@addmode{%
      \expandafter\pgfsyssoftpath@getcurrentpath\csname qrr@tikzpath@\pgfpictureid @#1\endcsname
      \immediate\write\pgfutil@auxout{%
        \noexpand\expandafter\gdef\noexpand\csname qrr@tikzpath@\pgfpictureid @#1\endcsname{%
          \expandafter\expandafter\expandafter\unexpanded
          \expandafter\expandafter\expandafter{\csname qrr@tikzpath@\pgfpictureid @#1\endcsname}}}}%
  },
  remembered path/.code={%
    \pgfutil@IfUndefined{qrr@tikzpath@\pgfpictureid @#1}{}{%
      \tikz@addmode{%
        \expandafter\pgfsyssoftpath@setcurrentpath
        \expandafter{\csname qrr@tikzpath@\pgfpictureid @#1\endcsname}}}}}
\makeatother
\begin{document}
\begin{tikzpicture}
\fill[left color=blue, right color=green] (0,0) rectangle (2,1);
\clip[remembered path=box];
\draw (0,0) -- (2,1);
\path[remember path=box] (0.5, 0) rectangle (1.5, 1) [reverse clip];
\end{tikzpicture}
\end{document}

Output

First compilation

enter image description here

Second compilation

enter image description here

3
  • I'm seeing a save to aux key with the spath3 library but … it doesn't write anything to the aux file unless I use it after the path (and not on it). ☹ Dec 4, 2022 at 20:49
  • Very interesting! Why +-16000pt? Is it the largest possible dimension?
    – JeanPierre
    Dec 5, 2022 at 20:17
  • No, it's not. Technically, it's 16384pt (that's the maximum a length could be in TeX) but PGF itself uses only 16000 for a few internals – mainly bounding box stuff which might lead to weird things when using coordinates that lie outside this range. Of course, this doesn't apply here anyway because we want to ignore the bounding box to be updated in the first place. By the way, 16000pt are well over 5 meters. This should be plenty enough for most documents. Dec 5, 2022 at 21:31

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .