我有一个可以独立工作的图表,我必须在文档中缩放此图表(请参阅下面的代码)。我从这篇文章中获得了灵感(将 tikz 图形缩放到 \textwidth 的百分比) 可以在更简单的绘图上使用(我认为这仅仅因为这个原因而有效,但似乎不能很好地推广到更复杂的形式)。
我希望像从磁盘加载的任何其他图形(pdf,png)一样方便地缩放它,但我收到了错误消息:
Package tikzscale Error: Requested to scale unscalable graphic.
由于我在文档中多次使用该图表,是否可以将其包装在命令中?
这里是代码:
\usepackage{tikz}
\usetikzlibrary{arrows}
\usepackage{tikz}
\usepackage{tikzscale}
\usepackage{filecontents}
\begin{document}
\begin{filecontents*}{test.tikz}
\begin{tikzpicture}[->,>=stealth',shorten >=1pt,auto,node distance=3cm,
thick,main node/.style={circle,fill=black,draw=none,text=white,font=\LARGE\bfseries}]
% nodes
\node[main node] (A) {L1};
\node[main node] (B) [below left of=A] {L2};
\node[main node] (C) [below right of=A] {L3};
\node[main node] (D) [below right of=B] {L4};
\node[main node] (E) [right of=C] {L5};
\node[main node] (F) [above of=E] {L6};
% lines
\path[every node/.style={font=\sffamily\large}]
(A) edge [bend right] node [right] {0.0} (B)
(B) edge [loop left] node {0.0} (B)
edge [bend right] node [right] {0.0} (D)
edge [bend right] node[below] {0.0} (C)
(C) edge [bend right] node [left] {0.0} (A)
edge [bend right] node [below] {0.0} (B)
edge [bend left] node [left] {0.0} (D)
edge [right] node [below] {0.0} (E)
(D) edge [loop below] node {0.0} (D);
\end{tikzpicture}
\end{filecontents*}
\begin{figure}[htb]
\centering
\includegraphics[width=0.2\textwidth]{test.tikz}
\caption{This figure has standard width}
\end{figure}
\begin{figure}[htb]
\centering
\includegraphics[width=0.4\textwidth]{test.tikz}
\caption{This figure has double width}
\end{figure}
\end{document}
答案1
缩放tikzscale
(线宽、字体不缩放)
软件包tikzscale
添加了选项scale
,可将图片缩放到所需的大小。但是,如果设置了该选项,而图形的大小与之前相同,则会抛出错误消息,即图形不可调整大小。
您也可以通过添加 来测试这一点scale=2
。如果图片的大小与 的默认值相同1
,那么您就没那么幸运了,必须重新设计图片以使其可缩放。
以下示例将\pgftransformscale
比例因子存储在全局宏中\ScaleFactor
。然后使用此因子缩放节点距离:
\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{arrows}
\usetikzlibrary{bending}% nicer bended arrow heads.
\usetikzlibrary{calc}
\usepackage{tikzscale}
\usepackage{filecontents}
\let\OrgPgfTransformScale\pgftransformscale
\renewcommand*{\pgftransformscale}[1]{%
\gdef\ScaleFactor{#1}%
\OrgPgfTransformScale{#1}%
}
\def\ScaleFactor{1}
\begin{document}
\begin{filecontents*}{test.tikz}
\begin{tikzpicture}[
->,
>=stealth',
shorten >=1pt,
auto,
node distance=3cm*\ScaleFactor,
thick,
main node/.style={
circle,
fill=black,
draw=none,
text=white,
font=\LARGE\bfseries
}
]
\typeout{* Scale factor: \ScaleFactor}
% nodes
\node[main node] (A) {L1};
\node[main node] (B) [below left of=A] {L2};
\node[main node] (C) [below right of=A] {L3};
\node[main node] (D) [below right of=B] {L4};
\node[main node] (E) [right of=C] {L5};
\node[main node] (F) [above of=E] {L6};
% lines
\path[every node/.style={font=\sffamily\large}]
(A) edge [bend right] node [right] {0.0} (B)
(B) edge [loop left] node {0.0} (B)
edge [bend right] node [right] {0.0} (D)
edge [bend right] node[below] {0.0} (C)
(C) edge [bend right] node [left] {0.0} (A)
edge [bend right] node [below] {0.0} (B)
edge [bend left] node [left] {0.0} (D)
edge [right] node [below] {0.0} (E)
(D) edge [loop below] node {0.0} (D);
\end{tikzpicture}
\end{filecontents*}
\begin{figure}
\centering
\includegraphics[width=\textwidth]{test.tikz}
\caption{This figure has standard width}
\end{figure}
\begin{figure}
\centering
\includegraphics[width=0.75\textwidth]{test.tikz}
\caption{This figure has double width}
\end{figure}
\end{document}
消息的最后比例因子\typeout
:
第一张图片:1.3079
第二张图片:0.88972
还可以\ScaleFactor
用于减小字体大小,例如:
font=\bfseries
\ifdim\ScaleFactor pt>0.95pt\LARGE\else
\ifdim\ScaleFactor pt>0.9pt\Large\else
\ifdim\ScaleFactor pt>0.85pt\large\else
\ifdim\ScaleFactor pt>0.8pt\normalsize\else
\small
\fi\fi\fi\fi,
然后结果
\begin{figure}
\centering
\includegraphics[width=0.6\textwidth]{test.tikz}
\caption{This figure has double width}
\end{figure}
第二张图片是:
通过缩放\resizebox
(所有内容都缩放)
如果您想以相同的方式缩放整个图片,\includegraphics
则会缩放普通图像,这也意味着线宽、字体等也会缩放,然后\resizebox
有助于:
\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{arrows}
\usepackage{tikz}
\usepackage{filecontents}
\begin{document}
\begin{filecontents*}{test.tikz}
\begin{tikzpicture}[->,>=stealth',shorten >=1pt,auto,node distance=3cm,
thick,main
node/.style={circle,fill=black,draw=none,text=white,font=\LARGE\bfseries}]
% nodes
\node[main node] (A) {L1};
\node[main node] (B) [below left of=A] {L2};
\node[main node] (C) [below right of=A] {L3};
\node[main node] (D) [below right of=B] {L4};
\node[main node] (E) [right of=C] {L5};
\node[main node] (F) [above of=E] {L6};
% lines
\path[every node/.style={font=\sffamily\large}]
(A) edge [bend right] node [right] {0.0} (B)
(B) edge [loop left] node {0.0} (B)
edge [bend right] node [right] {0.0} (D)
edge [bend right] node[below] {0.0} (C)
(C) edge [bend right] node [left] {0.0} (A)
edge [bend right] node [below] {0.0} (B)
edge [bend left] node [left] {0.0} (D)
edge [right] node [below] {0.0} (E)
(D) edge [loop below] node {0.0} (D);
\end{tikzpicture}
\end{filecontents*}
\begin{figure}[htb]
\centering
\resizebox{.2\textwidth}{!}{\input{test.tikz}\unskip}
\caption{This figure has standard width}
\end{figure}
\begin{figure}[htb]
\centering
\resizebox{.4\textwidth}{!}{\input{test.tikz}\unskip}
\caption{This figure has double width}
\end{figure}
\end{document}
评论:
- 之后
\unskip
用\input{test.tikz}
删除最后一行行尾的空格\end{tikzpicture}
。
答案2
\begin{frame}
\frametitle{Ranking on the Web}
\framesubtitle{PageRank Example}
\centering
\resizebox{.5\textwidth}{!}{\MyExample}
\end{frame}
其中 MyExample 是 tikzpicture...
解决方案比想象中简单得多,我只想让它平等地缩放所有内容,Heiko Oberdiek 的提示帮助我在某个地方找到了它。谢谢!