根据变量改变 tikz 坐标

根据变量改变 tikz 坐标

如何定义依赖于变量的坐标(变量改变时坐标自动改变)

以下是一个例子

\documentclass[12pt,a4paper]{article}
\usepackage{tikz}

\pagestyle{empty}

\begin{document}

\begin{tikzpicture}[remember picture,overlay]
\def\border{2cm}
\coordinate (A) at ([shift={(\border,\border)}]current page.south west);
\coordinate (B) at ([shift={(-\border,-\border)}]current page.north east);
\draw[thick,rounded corners] (A) rectangle (B);
\def\border{1cm}
%\coordinate (A) at ([shift={(\border,\border)}]current page.south west);
%\coordinate (B) at ([shift={(-\border,-\border)}]current page.north east);
\draw[thick,rounded corners,double,red] (A) rectangle (B);
\end{tikzpicture}

\end{document}

我希望有一种方法可以修改坐标A以及B何时\border改变而不必每次都重新定义这些坐标。

答案1

\documentclass[tikz]{standalone}
\begin{document}
\begin{tikzpicture}[put ab/.code={%
\coordinate (A) at ([shift={(#1,#1)}]current page.south west);%
\coordinate (B) at ([shift={(-#1,-#1)}]current page.north east);%
}]
\draw[thick,rounded corners,put ab=2cm] (A) rectangle (B);
% only need to use put ab when you want to change a b otherwise they will remain
\draw[thick,rounded corners,put ab=1cm] (A) rectangle (B);
\end{tikzpicture}
\end{document}

相关内容