[Tex/LaTex] Included PDF line width


I've an image I made with Inkscape and exported to PDF that I want to include in my document. However, there is one annoying thing: The line width (e.g. the border width of a rectangle) depends on the scaling of the PDF. For example, when I include it two times like this


the lines in the upper image are thicker than in the lower.

Can I override the line width somehow such that it is always, say, 1mm, regardless of the scaling or (re)define the base that is used to calculate the actucal width?

Best Answer

In general the answer is no. Very often the line width is explicitly set in the .pdf file. This setting will overwrite any setting that the TeX file might have set before.

With knowledge of PDF internals and PDF page stream operators, sometimes the .pdf file can be tweaked. Lets take a simple PostScript file:

%%BoundingBox: 99 99 121 111
%%HiResBoundingBox: 99.499989 99.499989 120.500012 110.499958
100 100 moveto 20 0 rlineto 0 10 rlineto
-20 0 rlineto closepath
100 100 moveto
10 5 rlineto

Then the program epstopdf calls ghostscript and generates a .pdf file. Since we need to take a look at the page stream, it can be uncompressed by

pdftk Example1.pdf cat output Example2.pdf uncompress

The page stream can be found via the /Contents key in the /Page object (here annotated by comments, sometimes with corresponding PostScript operators):

q 0.1 0 0 0.1 0 0 cm % save 0.1 scale
/R7 gs               % not relevant here
10 w                 % 10 setlinewidth
0 G                  % black for stroking
10 10 m              % m = moveto
210 10 l             % l = lineto
210 110 l
10 110 l
h                    % closepath
10 10 m
110 60 l
S                    % stroke
Q                    % restore

Here ghostscript has added a line width setting, the PostScript file didn't set the line width. It can be removed by commenting or overwriting with spaces. It is very important that the size of the object is not changed, otherwise many other settings needs updating:

  • The length of the stream is recorded in the key /Length in the dict stream object.
  • The offsets of the indirect objects are recorded in the xref table.

Example for disabling the line width setting (% is the comment char in both PostScript and PDF):

%0 w

Optionally the modified .pdf file can be recompressed, see above the command line with pdftk.

The following example uses pdfTeX. Since we do not want to keep the changed settings after the graphics (and to surprise pdfTeX), the changed settings are put between a save/restore pair, in pdfTeX: \pdfsave/\pdfrestore. However this also affects the current point, the two primitives must be called at the exact same point. Otherwise the synchronization of TeX internal coordinate system and the coordinate system of the PDF output is lost.

\usepackage[a4paper, hmargin=60mm, vmargin=0mm]{geometry}
\section*{Unmodifed example}



\section*{Example without line width}



\section*{Example with settings}


  10 w % setlinewidth
  1 J  % setlinecap: round
  1 j  % setlinejoin: round

  20 w % setlinewidth
  1 J  % setlinecap: round
  1 j  % setlinejoin: round



Several problems/drawbacks can be seen:

  • The media size/bounding box of the included .pdf file is not changed. That is seen by TeX. If the line width is changed, then the bounding box might change. Option trim can help here: positive values, if the line width is smaller than the original or negative values if the bounding box increases. Sometimes the values can be easily calculated, otherwise it can be measured visually (e.g.: \fbox with \setlength{\fboxsep}{0pt}). Depending on the image, the values can even be different for the four sides.

  • Even, if the line width is the same, the scaled image can look quite different, because the proportions from line width and shape are different, if the shape is scaled.