How to issue a warning when something is clipped in TikZ?

How to issue a warning when something is clipped in TikZ?

As the title states, I'd like to know if it's possible to issue a LaTeX warning if something was clipped in a tikzpicture. And, if so, how would I achieve this?

My use case is that I'm working on a small set of macros for producing drawings and I'd like a warning if I've drawn anything outside of a certain defined drawing area. I use clipping in the drawing to prevent anything outside the area from being drawn. This prevents the page layout from getting messed up, but the user has no idea that something was not actually put on the page (apart from manually inspecting the output). It'd be nice to give the user a heads up in my particular use case.

Ideally this warning would be enabled by putting a style, say clip warn, on a scope environment or the clip \path itself.

Here's a MWE for a starting point and for testing purposes:

\documentclass{article}
\usepackage{tikz}
\tikzset{x=1em,y=1em}

\tikzset{
  clip warn/.style = {
    % insert magic code here
  },
}

\begin{document}

This diagram should not produce a warning: 
  \tikz{\path[clip] (0,0) rectangle (1,1); \draw (0.25,0.25) -- (0.75,0.75)}

This diagram should produce a warning: 
  \tikz{\path[clip] (0,0) rectangle (1,1); \draw (-0.25,0.25) -- (0.25,0.75)}

\end{document}

enter image description here

답변1

Revision. This is a snippet that computes the intersections with the bounding box, and issues a warning if there is one. (Clearly, if you have a path that is entirely outside the bounding box, no warning will be issued.) In this version, one has to place \IssueWarnings at the end of the picture(s), and I have no idea why execute at end picture={\IssueWarning} fails. Any information on this will be highly appreciated.

\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{intersections}
\tikzset{x=1em,y=1em}
\newcounter{Paths}
\makeatletter% from https://tex.stackexchange.com/a/5354/121799
\tikzset{nomorepostaction/.code=\let\tikz@postactions\pgfutil@empty
\stepcounter{Paths}}
\newcommand\CheckIfPathExists[1]{
\pgfutil@ifundefined{tikz@intersect@path@name@#1}{\global\def\myPathExists{0}}{\global\def\myPathExists{1}}
}
\makeatother
\newcommand{\IssueWarnings}{\CheckIfPathExists{clip}
\ifnum\myPathExists=1
\xdef\CurrentPaths{\thePaths}
  \foreach \i in {2,...,\CurrentPaths}
  {\fill [name intersections={of=clip and path\i, name=i, total=\t}]
    %[red, opacity=0.5, every node/.style={above left, black, opacity=1}]
 \pgfextra{\xdef\Warn{\t}};
    }
    \ifnum\Warn>0\typeout{Warning: some paths got clipped}\fi
\else
\typeout{clip path not found}
\fi
}

\tikzset{
  clip warn/.style = {clip,name path=clip},
  every picture/.style={%
    execute at begin picture={\setcounter{Paths}{0}},
    execute at end picture={%
%   \IssueWarnings %<- doesn't work and I dunno why
    },
    },
 every path/.style={postaction={nomorepostaction,draw,
    name path=path\thePaths}}}
\begin{document}

This diagram should not produce a warning: 
  \tikz{\path[clip warn] (0,0) rectangle (1,1); 
  \draw (0.25,0.25) -- (0.25,0.75);
  \IssueWarnings
}

This diagram should produce a warning:%\typeout{checking second picture}
  \tikz{\path[clip warn] (0,0) rectangle (1,1); 
  \draw (-0.25,0.25) -- (0.25,0.75);
  \IssueWarnings
}

\end{document}

관련 정보