![缩放并居中独立 tikz 图片](https://linux22.com/image/345887/%E7%BC%A9%E6%94%BE%E5%B9%B6%E5%B1%85%E4%B8%AD%E7%8B%AC%E7%AB%8B%20tikz%20%E5%9B%BE%E7%89%87.png)
我的 tex 文档中出现了奇怪的行为。我有一个包含多个 tex 文件的大型项目。对于我的 tikz 图片,我使用独立文档类并通过以下方式包含它们
\begin{figure}[!ht]
\centering
\includestandalone[mode=buildnew, width=1.0\textwidth]{tikzPicture}
\caption{caption}
\label{fig:tikzPicture}
\end{figure}
问题是,图片变得比\textwidth
我尝试缩放它们时的小scale=1.5
,而不是width=1.0\textwidth
它们会左移并且不再居中。
我的一张 tikz 图片如下所示:
\documentclass{standalone}
\usepackage{graphics}
\usepackage{tikz}
\usepackage{tikzscale}
\usepackage{verbatim}
\usepackage[active,tightpage]{preview}
\usepackage{etoolbox}
\usepackage{ifthen}
\usepackage{rotating}
\PreviewEnvironment{tikzpicture}
\setlength\PreviewBorder{10pt}
\usetikzlibrary{shadows}
\usetikzlibrary{arrows,shapes}
\usetikzlibrary{calc}
\usetikzlibrary{positioning}
\xdefinecolor{midnightBlue}{RGB}{25,25,112}
\xdefinecolor{navy}{RGB}{0,0,128}
\xdefinecolor{fireblick}{RGB}{178,34,34}
\begin{document}
\begin{tikzpicture}[
scale=1,
node distance=0mm,
desc/.style={
rectangle,
rounded corners,
draw=black,
very thick,
text centered,
text width=7cm,
minimum height=12mm,
fill=midnightBlue!30,
drop shadow
},
hByte/.style={
rectangle,
rounded corners=1pt,
draw=black,
thick,
text centered,
text width=0.6cm,
minimum height=28mm,
fill=midnightBlue!30,
xshift=0.45cm,
drop shadow
},
Byte/.style={
rectangle,
rounded corners=1pt,
draw=black,
thick,
text centered,
text width=0.6cm,
minimum height=56mm,
fill=midnightBlue!30,
xshift=0.45cm,
drop shadow
},
qByte/.style={
rectangle,
rounded corners=1pt,
draw=black,
thick,
text centered,
text width=2.6cm,
minimum height=10mm,
fill=midnightBlue!30,
xshift=1.45cm,
drop shadow
},
it/.style={
fill=blue!10
},
level1/.style={
minimum height=5mm,
text width=10cm,
text centered
},
every node/.style={font=\sffamily}
]
\node at (0cm,0cm) [] (base) {};
%-------------------------------------------------------------------
%draw level 1
\node at ($(base)+(-7cm,3cm)$) [desc, it, text width =4.7cm, xshift=2.5cm] (header) {...};
\node at ($(header.east)-(0cm,0cm)$) [desc, it, text width =6.7cm, xshift=3.5cm] (body) {timeSource};
\node at ($(body.east)-(0cm,0cm)$) [desc, it, text width =2.7cm, xshift=1.5cm] (tlv) {path trace TLV};
%-------------------------------------------------------------------
%draw segments
\def\arrowOffsetOne{0cm,2cm};
\def\arrowOffsetTwo{0cm,1cm};
\draw [darkgray,thin,<->] ($(header.north west)+(\arrowOffsetOne)$) -- ($(tlv.north east)+(\arrowOffsetOne)$) node [black,level1, midway, above=0.1cm] (frame) {Announce Nachricht};
\draw [darkgray,thin,<->] ($(header.north west)+(\arrowOffsetTwo)$) -- ($(header.north east)+(\arrowOffsetTwo)$) node [black,level1, midway, above=0.1cm] (headerLength) {29 Byte};
\draw [darkgray,thin,<->] ($(body.north west)+(\arrowOffsetTwo)$) -- ($(body.north east)+(\arrowOffsetTwo)$) node [black,level1, midway, above=0.1cm] (bodyLength) {1 Byte};
\draw [darkgray,thin,<->] ($(tlv.north west)+(\arrowOffsetTwo)$) -- ($(tlv.north east)+(\arrowOffsetTwo)$) node [black,level1, midway, above=0.1cm] (tlvLenght) {\textgreater
4 + 8N Byte};
%-------------------------------------------------------------------
%draw Header
\node at ($(base)+(-5cm,-3cm)$) [qByte, it, fill=orange!20] (node1) {tlvType};
\node at ($(node1.east)-(0.025cm,0cm)$) [qByte, it, fill=red!20] (node2) {lengthField};
\node at ($(node2.east)-(0.025cm,0cm)$) [qByte, it, fill=green!20] (lastnode) {pathSequence};
%-------------------------------------------------------------------
%draw separating lines
\draw [gray,dashed,thin] ($(header.north west)+(\arrowOffsetOne)+(0cm,1cm)$) -- (header.south west);
\draw [gray,dashed,thin] (tlv.south west) -- (node1.north west);
\draw [gray,dashed,thin] ($(body.north west)+(\arrowOffsetTwo)$) -- (body.south west);
\draw [gray,dashed,thin] ($(body.north east)+(\arrowOffsetTwo)$) -- (body.south east);
\draw [gray,dashed,thin] ($(tlv.north east)+(\arrowOffsetOne)+(0cm,1cm)$) -- (tlv.south east);
\draw [gray,dashed,thin] (tlv.south east) -- (lastnode.north east);
\draw [gray,dashed,thin] (node1.south west) -- ($(node1.south west)-(0,0.3)$);
\draw [gray,dashed,thin] (node1.south east) -- ($(node1.south east)-(0,0.3)$);
\draw [gray,dashed,thin] (node2.south east) -- ($(node2.south east)-(0,0.3)$);
\draw [gray,dashed,thin] (lastnode.south east) -- ($(lastnode.south east)-(0,0.3)$);
\node at ($(node1.south west)-(0.5,0.7)$) [black, level1] {Byte};
\draw [darkgray,thin,<->] ($(node1.south west)-(0,0.3)$) -- ($(node1.south east)-(0,0.3)$) node [black,level1, midway, below=0.1cm] {2};
\draw [darkgray,thin,<->] ($(node2.south west)-(0,0.2)$) -- ($(node2.south east)-(0,0.2)$) node [black,level1, midway, below=0.175cm] {2};
\draw [darkgray,thin,<->] ($(lastnode.south west)-(0,0.3)$) -- ($(lastnode.south east)-(0,0.3)$) node [black,level1, midway, below=0.1cm] {8N};
%-------------------------------------------------------------------
\end{tikzpicture}
\end{document}
有人知道是什么原因导致这种现象吗?问候,Efim
解决方案:正如 esdd 在其评论中所说,解决方案非常简单。出现缩放问题的原因是text width=10cm
其中的设置level1
放大了整个图片。删除此设置即可解决问题。
答案1
关键text width=10cm
在于
level1/.style={
minimum height=5mm,
text width=10cm,
text centered
},
扩大图片的宽度。我觉得代码中不需要这个键。删除此设置即可获得
代码:
\documentclass{standalone}
\usepackage{graphics}
\usepackage{tikz}
\usepackage{tikzscale}
\usepackage{verbatim}
\usepackage[active,tightpage]{preview}
\usepackage{etoolbox}
\usepackage{ifthen}
\usepackage{rotating}
\PreviewEnvironment{tikzpicture}
\setlength\PreviewBorder{10pt}
\usetikzlibrary{shadows}
\usetikzlibrary{arrows,shapes}
\usetikzlibrary{calc}
\usetikzlibrary{positioning}
\xdefinecolor{midnightBlue}{RGB}{25,25,112}
\xdefinecolor{navy}{RGB}{0,0,128}
\xdefinecolor{fireblick}{RGB}{178,34,34}
\begin{document}
\begin{tikzpicture}[
scale=1,
node distance=0mm,
desc/.style={
rectangle,
rounded corners,
draw=black,
very thick,
text centered,
text width=7cm,
minimum height=12mm,
fill=midnightBlue!30,
drop shadow
},
hByte/.style={
rectangle,
rounded corners=1pt,
draw=black,
thick,
text centered,
text width=0.6cm,
minimum height=28mm,
fill=midnightBlue!30,
xshift=0.45cm,
drop shadow
},
Byte/.style={
rectangle,
rounded corners=1pt,
draw=black,
thick,
text centered,
text width=0.6cm,
minimum height=56mm,
fill=midnightBlue!30,
xshift=0.45cm,
drop shadow
},
qByte/.style={
rectangle,
rounded corners=1pt,
draw=black,
thick,
text centered,
text width=2.6cm,
minimum height=10mm,
fill=midnightBlue!30,
xshift=1.45cm,
drop shadow
},
it/.style={
fill=blue!10
},
level1/.style={
minimum height=5mm,
%text width=10cm,% <- remove this
text centered
},
every node/.style={font=\sffamily}
]
\node at (0cm,0cm) [] (base) {};
%-------------------------------------------------------------------
%draw level 1
\node at ($(base)+(-7cm,3cm)$) [desc, it, text width =4.7cm, xshift=2.5cm] (header) {...};
\node at ($(header.east)-(0cm,0cm)$) [desc, it, text width =6.7cm, xshift=3.5cm] (body) {timeSource};
\node at ($(body.east)-(0cm,0cm)$) [desc, it, text width =2.7cm, xshift=1.5cm] (tlv) {path trace TLV};
%-------------------------------------------------------------------
%draw segments
\def\arrowOffsetOne{0cm,2cm};
\def\arrowOffsetTwo{0cm,1cm};
\draw [darkgray,thin,<->] ($(header.north west)+(\arrowOffsetOne)$) -- ($(tlv.north east)+(\arrowOffsetOne)$) node [black,level1, midway, above=0.1cm] (frame) {Announce Nachricht};
\draw [darkgray,thin,<->] ($(header.north west)+(\arrowOffsetTwo)$) -- ($(header.north east)+(\arrowOffsetTwo)$) node [black,level1, midway, above=0.1cm] (headerLength) {29 Byte};
\draw [darkgray,thin,<->] ($(body.north west)+(\arrowOffsetTwo)$) -- ($(body.north east)+(\arrowOffsetTwo)$) node [black,level1, midway, above=0.1cm] (bodyLength) {1 Byte};
\draw [darkgray,thin,<->] ($(tlv.north west)+(\arrowOffsetTwo)$) -- ($(tlv.north east)+(\arrowOffsetTwo)$) node [black,level1, midway, above=0.1cm] (tlvLenght) {\textgreater
4 + 8N Byte};
%-------------------------------------------------------------------
%draw Header
\node at ($(base)+(-5cm,-3cm)$) [qByte, it, fill=orange!20] (node1) {tlvType};
\node at ($(node1.east)-(0.025cm,0cm)$) [qByte, it, fill=red!20] (node2) {lengthField};
\node at ($(node2.east)-(0.025cm,0cm)$) [qByte, it, fill=green!20] (lastnode) {pathSequence};
%-------------------------------------------------------------------
%draw separating lines
\draw [gray,dashed,thin] ($(header.north west)+(\arrowOffsetOne)+(0cm,1cm)$) -- (header.south west);
\draw [gray,dashed,thin] (tlv.south west) -- (node1.north west);
\draw [gray,dashed,thin] ($(body.north west)+(\arrowOffsetTwo)$) -- (body.south west);
\draw [gray,dashed,thin] ($(body.north east)+(\arrowOffsetTwo)$) -- (body.south east);
\draw [gray,dashed,thin] ($(tlv.north east)+(\arrowOffsetOne)+(0cm,1cm)$) -- (tlv.south east);
\draw [gray,dashed,thin] (tlv.south east) -- (lastnode.north east);
\draw [gray,dashed,thin] (node1.south west) -- ($(node1.south west)-(0,0.3)$);
\draw [gray,dashed,thin] (node1.south east) -- ($(node1.south east)-(0,0.3)$);
\draw [gray,dashed,thin] (node2.south east) -- ($(node2.south east)-(0,0.3)$);
\draw [gray,dashed,thin] (lastnode.south east) -- ($(lastnode.south east)-(0,0.3)$);
\node at ($(node1.south west)-(0.5,0.7)$) [black, level1] {Byte};
\draw [darkgray,thin,<->] ($(node1.south west)-(0,0.3)$) -- ($(node1.south east)-(0,0.3)$) node [black,level1, midway, below=0.1cm] {2};
\draw [darkgray,thin,<->] ($(node2.south west)-(0,0.2)$) -- ($(node2.south east)-(0,0.2)$) node [black,level1, midway, below=0.175cm] {2};
\draw [darkgray,thin,<->] ($(lastnode.south west)-(0,0.3)$) -- ($(lastnode.south east)-(0,0.3)$) node [black,level1, midway, below=0.1cm] {8N};
%-------------------------------------------------------------------
\end{tikzpicture}
\end{document}