我有很多小的 tikz 图形,我都想缩小它们,所以我想把它们放在一个范围内,如下所示:
\documentclass{article}
\usepackage{tikz}
\begin{document}
\begin{scope}[scale=0.3]
\begin{tikzpicture}
% Draw stuff in a small scale
\end{tikzpicture}
\begin{tikzpicture}
% Draw more stuff in a small scale
\end{tikzpicture}
\end{scope}
\begin{tikzpicture}
% Draw stuff in normal scale again
\end{tikzpicture}
\end{document}
但这不起作用,因为范围环境仅在 tikzpicture 内部定义。
我怎样才能实现这种效果?我是否必须使用\tikzset
并保存当前比例,然后缩小比例并在最后再次恢复,或者是否有“正确”的方法来做到这一点?
答案1
我们可以用\tikzset{every picture/.style={scale=0.3}}
它实现这个目的。我附上了一个示例和页面预览,其中缩放比例从 0.1 到 1.2,步长为 0.1,以演示其用法。
%! *latex tikz-scaling-a.tex
\documentclass[a4paper]{article}
\pagestyle{empty}
\usepackage{tikz}
\parindent=0pt
\addtolength{\textheight}{1in}
\begin{document}
\def\malpicture{% Picture is changing, the font is not...
\begin{tikzpicture}[x=1cm, y=1cm]
\draw[line width=2pt, ](0,0)--(5,2);
\node at (1,1) {Hello World!};
\end{tikzpicture}
}% End of \malpicture...
% A typical use...
\malpicture\par
% And now with scaling...
\foreach\scaling in {0.1,0.2,...,1.2} {%
\tikzset{every picture/.style={scale=\scaling}}% =0.3 etc.
\fbox{\malpicture}\par
}% End of \foreach\scaling...
\tikzset{every picture/.style={scale=0.3}}
\malpicture
\end{document}
我附上了一个类似的例子,其中使用TeX 组(括号或\begingroup
和)来限制命令。第一张和最后一张图片没有变化,第二张和第三张受到命令的影响。\endgroup
\tikzset
\tikzset
%! *latex tikz-scaling-b.tex
\documentclass[a4paper]{article}
\pagestyle{empty}
\usepackage{tikz}
\parindent=0pt
\addtolength{\textheight}{1in}
\begin{document}
% A normal picture before a change is applied...
\begin{tikzpicture}[x=1cm, y=1cm]
\draw[line width=2pt, ](0,0)--(5,2);
\node at (1,1) {Hello World!};
\end{tikzpicture}
% A group will limit the \tikzset command...
\begingroup % or we use opening brace "{"
\tikzset{every picture/.style={scale=0.3}} % a change in parameters
\begin{tikzpicture}[x=1cm, y=1cm]
\draw[line width=2pt, ](0,0)--(5,2);
\node at (1,1) {My first scaled picture!};
\end{tikzpicture}\par
\begin{tikzpicture}[x=1cm, y=1cm]
\draw[line width=2pt, ](0,0)--(5,2);
\node at (1,1) {My second scaled picture!};
\end{tikzpicture}%
\endgroup % or we use closing brace "}"
% A let's get back to normal scaling...
\begin{tikzpicture}[x=1cm, y=1cm]
\draw[line width=2pt, ](0,0)--(5,2);
\node at (1,1) {My last picture!};
\end{tikzpicture}
\end{document}