1

I'm having a strange behavior whithin my tex document. I have a large project of several tex files. For my tikz pictures I use the standalone document class and include them via

\begin{figure}[!ht]
    \centering
     \includestandalone[mode=buildnew, width=1.0\textwidth]{tikzPicture}
      \caption{caption}
    \label{fig:tikzPicture}
\end{figure}

The issue is, that the pictures get smaller than \textwidth and if I try to scale them with scale=1.5 instead of width=1.0\textwidth they get left shifted and are not centered any more.

One of my tikz pictures looks like following:

\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}

Does anybody know what could cause this behavior? Regards, Efim

Solution: As esdd already told in his comment, the solution is very simple. The reason for that scaling issue was that the setting text width=10cm whithin level1 was enlarging the whole picture. Removing this setting solves the issue.

  • Welcome to TeX.SX! Try using mode=image instead of buildnew. I think buildnew uses the picture code and image uses the produced Image. The difference is that tikzpictures do not have a width option but Images have... (I'm not sure, this is more of a hunch) – Guilherme Zanotelli Dec 12 '16 at 8:33
  • Have you noticed that there is a lot of whitespace on the sides of the diagram? If you fix that, perhaps you get an appropriate scale with width=\textwidth. If you make it wider than \textwidth (as happens with scale=1.5, as far as I can see), the image will stick out into the right margin, or even out of the page. If you want it centered on the textblock you can use \makebox or adjustbox as discussed in tex.stackexchange.com/questions/39435, but modifying the diagram might be just as good a solution. – Torbjørn T. Dec 12 '16 at 8:38
  • There are many excellent users of LaTeX, here. I can suggest TikzEdt to verificate your Tikz code. Look the link: tikzedt.org. – Sebastiano Dec 12 '16 at 8:39
  • Thanks for your comments. @GuilhermeZanotelli: changing the mode to mode=image doesn't change the resulting picture. – Efim Dec 12 '16 at 8:50
  • 1
    The text width=10cm for the style level1 enlarges your picture on the left and right side. So there is additional whitespace. Remove this setting for level1 to get the desired result. – esdd Dec 12 '16 at 8:58
3

The key text width=10cm in

level1/.style={
    minimum height=5mm,
    text width=10cm,
    text centered
},

enlarges the width of your picture. It seems to me that this key in not needed in the code. Remove this setting to get

enter image description here

Code:

\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}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.