10

I've made two standalone documents that ought to be the same dimensions (page size), but pdfinfo reports the page sizes (dimensions) as being slightly different, and I can't for the life of me see why. The two documents are supposed to be the front and back of a print.

The included graphics are commented out, since they do not impact the document dimensions, and for reproducibility.

Front: (pdfinfo reports a size of 481.89 x 340.157 pts)

\documentclass{standalone}
\pagestyle{empty}
\usepackage{tikz} 
\usepackage{ifthen} 
\usepackage{graphicx} 
\renewcommand*\ttdefault{lmvtt}
\usepackage[letterspace=120]{microtype}
\renewcommand{\familydefault}{\ttdefault}
\begin{document}
\begin{tikzpicture}
\fill[white] (-1,-1) rectangle (16,11);
\draw[thick] (0,0) rectangle (15,10);
%\node at (3.5,9.4) {\includegraphics[scale=0.25]{logo.pdf}};

\end{tikzpicture}
\end{document}

Back: (pdfinfo reports a size of 482.687 x 340.556 pts)

\documentclass{standalone}
\pagestyle{empty}
\usepackage{tikz} 
\usepackage{ifthen} 
\usepackage{graphicx} 
\renewcommand*\ttdefault{lmvtt}
\usepackage[letterspace=120]{microtype}
\renewcommand{\familydefault}{\ttdefault}
\definecolor{blue}{HTML}{3c3475}
\begin{document}
\begin{tikzpicture}
\fill[blue] (-1,-1) rectangle (16,11);
\draw[gray] (7.5,-1) rectangle (7.5,11);
%\node at (3.5,7) {\includegraphics[scale=0.2]{dfp-logo.pdf}};
\end{tikzpicture}
\end{document}
  • You could draw an invisible rectangle of the desired size and use as bounding box. – Skillmon Apr 10 '18 at 11:26
  • @Skillmon This was what I was trying to accomplish with \fill[white] (-1,-1) rectangle (16,11); – martinmch Apr 10 '18 at 11:29
  • 1
    Put a use as bounding box in the options of that rectangle. – Skillmon Apr 10 '18 at 11:29
  • That works! If it was an answer, I'd accept it. – martinmch Apr 10 '18 at 11:34
12

The reason can be seen from this example:

\documentclass{standalone}
\usepackage{tikz}
\begin{document}    
\begin{tikzpicture}
\draw [line width=1cm] (0,0) -- (1,0);
\draw [red] (0,0) -- (1,0);

\draw [blue,line cap=rect] (-0.5\pgflinewidth,0) -- (-0.5cm+0.5\pgflinewidth,0);
\end{tikzpicture}    
\end{document}

enter image description here

The whitespace on the sides is, as you can see from the blue line, 0.5cm, so half the linewidth of the black line. Seems TikZ leaves room in the bounding box for a rect type line cap, even if the standard butt is used.

So in your examples, for the first you only have

\fill[white] (-1,-1) rectangle (16,11);

whereas in the second you also \draw paths to the edges of this rectangle. When you do e.g.

\draw[thick, white] (-1,-0.5) -- (16,-0.5);

this will therefore extend the bounding box by 0.4pt on both the left and right sides, as thick corresponds to line width=0.8pt. From pdfinfo, you see that the second one is about 0.8pt wider than the first one.

The 0.4pt difference in height comes similarly from

\draw[gray] (7.5,-1) rectangle (7.5,11);

which adds half the default line width, 0.4pt, at the top and bottom.

If you change the offending lines in the second diagram to

\draw[gray,line cap=rect] (7.5,-1cm+0.5\pgflinewidth) rectangle (7.5,11cm-0.5\pgflinewidth);
\draw[thick, white, line cap=rect] (-1cm+0.5\pgflinewidth,-0.5) -- (16cm-0.5\pgflinewidth,-0.5);
\draw[thick, white, line cap=rect] (-1cm+0.5\pgflinewidth,10.5) -- (16cm-0.5\pgflinewidth,10.5);

both PDFs become 481.89 x 340.157 pts according to pdfinfo.

  • Great explanation! This correctly identifies the problem with the code, and makes the use as bounding box option unnecessary. I change my accepted answer tot this, because I consider this solution less of a hack. – martinmch Apr 10 '18 at 11:52
  • 4
    @martinmch I won't consider use as bounding box a hack. It is added to TikZ on purpose for cases similar like this (most notably if you need control points for a curve which might lie far outside your actual picture). Your acceptance of this answer above mine stays reasonable and valid though. – Skillmon Apr 10 '18 at 11:58
  • I agree that hack is the wrong word here. – martinmch Apr 10 '18 at 12:05
8

My comments as an answer:

You can tell TikZ that the desired size of the tikzpicture is the size of a single object in that tikzpicture with the option use as bounding box. Adding this option to your \fill rectangles in both pictures lead to 481.89 x 340.157 pts for both of them in pdfinfo.

Code for front:

\documentclass{standalone}
\pagestyle{empty}
\usepackage{tikz} 
\usepackage{ifthen} 
\usepackage{graphicx} 
\renewcommand*\ttdefault{lmvtt}
\usepackage[letterspace=120]{microtype}
\renewcommand{\familydefault}{\ttdefault}
\begin{document}
\begin{tikzpicture}
\fill[white,use as bounding box] (-1,-1) rectangle (16,11);
\draw[thick] (0,0) rectangle (15,10);
%\node at (3.5,9.4) {\includegraphics[scale=0.25]{logo.pdf}};
\node at (5.8,9.12) {for};

%% Distance between each line should be changed from 0.03 to 0.04.
\draw[thick, densely dotted] (6.1,9.02) -- (13.5,9.02);
\draw[thick] (0.03,8.63) -- (14.97,8.63);
\draw[thin] (0.03,8.55) -- (14.97,8.55);
\draw[thick] (0.03,7.9) -- (14.97,7.9);

% Labels
\node at (0.5,8.225) {Tid};
\node at (2,8.19) {Mandag};
\node at (4,8.19) {Tirsdag};
\node at (6,8.19) {Onsdag};
\node at (9,8.19) {Torsdag};
\node at (11,8.19) {Fredag};
\node at (13,8.19) {L\o{}rdag};
\node at (14.5,8.225) {Tid};
\def\bordertext{Skole- og tegnemateriel, kollegiehefter etc.}
\node[rotate=-90] at (15.3,4.4) {\bordertext};
\node[rotate=90]  at (-0.3,4.4) {\bordertext};

\foreach \x in {1,3,5,7,8,10,12,14}{
    \def\topyone{8.53}; \def\lowyone{7.87};
    \def\topytwo{7.93}; \def\lowytwo{2.05}; % 0.03
    \ifthenelse{\x=1\OR\x=14}{
        \draw[thick] (\x,\topyone) -- (\x,\topytwo); % Top dividers
        \draw[thick] (\x,\lowyone) -- (\x,\lowytwo);  % Lower divider
    }{
        \draw[thin] (\x,\topyone) -- (\x,\topytwo); % Top dividers
        \draw[thin] (\x,\lowyone) -- (\x,\lowytwo); % Lower dividers
    }
}
% horizontal lines
\foreach \y in {7.25,6.6,5.95,5.30,4.65,4,3.35,2.7,2.05}{
       \draw[thin] (0.03, \y) -- (0.97, \y);
       \draw[thin] (1.03, \y) -- (2.97, \y);
       \draw[thin] (3.03, \y) -- (4.97, \y);
       \draw[thin] (5.03, \y) -- (6.97, \y);
       \draw[thin] (8.03, \y) -- (11.97,\y);
       \draw[thin] (12.03,\y) -- (13.97,\y);
       \draw[thin] (14.03,\y) -- (14.97,\y);
       }
\draw[thick] (0.03,2.05) -- (14.97,2.05);
\end{tikzpicture}
\end{document}

Code for back:

\documentclass{standalone}
\pagestyle{empty}
\usepackage{tikz} 
\usepackage{ifthen} 
\usepackage{graphicx} 
\renewcommand*\ttdefault{lmvtt}
\usepackage[letterspace=120]{microtype}
\renewcommand{\familydefault}{\ttdefault}
\definecolor{blue}{HTML}{3c3475}
\begin{document}
\begin{tikzpicture}
\fill[blue,use as bounding box] (-1,-1) rectangle (16,11);
\draw[gray] (7.5,-1) rectangle (7.5,11);
\draw[thick, white] (-1,-0.5) -- (16,-0.5);
\draw[thick, white] (-1,10.5) -- (16,10.5);
%\node at (3.5,7) {\includegraphics[scale=0.2]{dfp-logo.pdf}};
%\node at (11.5,8) {\includegraphics[scale=0.18]{title.pdf}};
%\node at (11.7,1.5) {\includegraphics[scale=0.18]{nameplate-line-white.pdf}};
\end{tikzpicture}
\end{document}
4

There is nothing that I can add to Torbjørn T.'s great and accurate explanation. However, I'd like to mention that the creator(s) of TikZ provided an arguably simpler remedy of this issue: pgfinterruptboundingbox. You can easily confirm that, when using this in your this example the bounding box will not increase.

\documentclass{standalone}
\pagestyle{empty}
\usepackage{tikz} 
\usepackage{ifthen} 
\usepackage{graphicx} 
\renewcommand*\ttdefault{lmvtt}
\usepackage[letterspace=120]{microtype}
\renewcommand{\familydefault}{\ttdefault}
\definecolor{blue}{HTML}{3c3475}
\begin{document}
\begin{tikzpicture}
\fill[blue] (-1,-1) rectangle (16,11);
\begin{pgfinterruptboundingbox}
\draw[gray] (7.5,-1) rectangle (7.5,11);
\end{pgfinterruptboundingbox}
%\node at (3.5,7) {\includegraphics[scale=0.2]{dfp-logo.pdf}};
\end{tikzpicture}
\end{document}

Note that this trick is particularly handy in situations in which one creates auxiliary paths for, say, intersections.

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.