TikZ 中出現剪輯時如何發出警告?

TikZ 中出現剪輯時如何發出警告?

正如標題所述,我想知道如果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}

相關內容