正如标题所述,我想知道如果在 中剪辑了某些内容,是否可以发出 LaTeX 警告tikzpicture
。如果可以,我该如何实现这一点?
我的用例是,我正在开发一小组用于绘制图形的宏,如果我绘制了超出某个定义的绘图区域的内容,我希望得到警告。我在绘图中使用剪辑来防止绘制超出该区域的内容。这可以防止页面布局混乱,但用户并不知道页面上实际上没有放置任何内容(除了手动检查输出)。在我的特定用例中,最好能给用户一个提示。
理想情况下,可以通过clip warn
在scope
环境或剪辑\path
本身上添加一种样式来启用此警告。
以下是用于起点和测试目的的 MWE:
\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}
答案1
修订。这是一个计算与边界框的交点的代码片段,如果有交点,则会发出警告。(显然,如果您的路径完全在边界框之外,则不会发出警告。)在这个版本中,必须将其放置\IssueWarnings
在图片的末尾,我不知道为什么execute at end picture={\IssueWarning}
会失败。任何有关此的信息都将不胜感激。
\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}