6

How to ensure that the set width of pgfplot figure is correctly applied in case I do not have a label?

Please consider the example below

\documentclass{article}

\usepackage{pgfplots,calc,filecontents,tikzscale}

\begin{filecontents*}{test.tikz}
    \begin{tikzpicture}%
        \begin{axis}[width=120pt,height=3cm]%
        \addplot[domain=-3:3] {sin(deg(x))};%
        \end{axis}%
    \end{tikzpicture}%
\end{filecontents*}

\begin{filecontents*}{test-label.tikz}
    \begin{tikzpicture}%
    \begin{axis}[width=120pt,height=3cm,ylabel=ylabel]%
    \addplot[domain=-3:3] {sin(deg(x))};%
    \end{axis}%
    \end{tikzpicture}%
\end{filecontents*}

\def\mymacro{\input{test.tikz}}
\def\mymacrolabel{\input{test-label.tikz}}

\def\mymacroscaleonlyaxis{\includegraphics[width=120pt,height=3cm]{test.tikz}}
\def\mymacrolabelscaleonlyaxis{\includegraphics[width=120pt,height=3cm]{test-label.tikz}}

\newlength{\test}
\begin{document}
    \mymacro%
    \settowidth{\test}{\mymacro}%

    \the\test

    \mymacrolabel%
    \settowidth{\test}{\mymacrolabel}%

    \the\test

    \mymacroscaleonlyaxis%
    \settowidth{\test}{\mymacroscaleonlyaxis}%

    \the\test

    \mymacrolabelscaleonlyaxis%
    \settowidth{\test}{\mymacrolabelscaleonlyaxis}%

    \the\test
\end{document}

The cases where a ylabel is set, are scaled almost correctly (ca. 118pt instead of the desired 120pt). But the figures without label are not scaled correctly in either case. How to fix that?

enter image description here

bonanza
  • 2,141
  • 2
  • 26
  • 37
  • Can you clarify how https://tex.stackexchange.com/a/422979/24974 does not address your question? – erik Apr 26 '19 at 20:14
  • @erik thanks for the pointer, when adding this to the code above, it removes whitespace around the (non-labeled) figure, but the width is then just approx. 75pt and not the desired 120pt. – bonanza Apr 26 '19 at 20:18

1 Answers1

5

You can combine trim axis left (applied to the tikzpicture) with scale only axis (applied to the axis). If you only trim the axis, pgfplots will still allocate 45pt of space for tick marks and axis labels. If you only scale the axis, the axis decorations will add to the overall width.

enter image description here

\documentclass{article}
\usepackage{pgfplots,calc,filecontents,tikzscale}
\pgfplotsset{compat=1.16}

\begin{filecontents*}{test.tikz}
    \begin{tikzpicture}%
        \begin{axis}[width=120pt,height=3cm]%
        \addplot[domain=-3:3] {sin(deg(x))};%
        \end{axis}%
    \end{tikzpicture}%
\end{filecontents*}

\begin{filecontents*}{test-label.tikz}
    \begin{tikzpicture}%
    \begin{axis}[width=120pt,height=3cm,ylabel=ylabel]%
    \addplot[domain=-3:3] {sin(deg(x))};%
    \end{axis}%
    \end{tikzpicture}%
\end{filecontents*}

\begin{filecontents*}{test.tikz.scaled}
    \begin{tikzpicture}[trim axis left]%
        \begin{axis}[width=120pt,height=3cm,scale only axis]%
        \addplot[domain=-3:3] {sin(deg(x))};%
        \end{axis}%
    \end{tikzpicture}%
\end{filecontents*}

\begin{filecontents*}{test-label.tikz.scaled}
    \begin{tikzpicture}[trim axis left]%
    \begin{axis}[width=120pt,height=3cm,ylabel=ylabel,scale only axis]%
    \addplot[domain=-3:3] {sin(deg(x))};%
    \end{axis}%
    \end{tikzpicture}%
\end{filecontents*}

\def\mymacro{\input{test.tikz}}
\def\mymacrolabel{\input{test-label.tikz}}
\def\mymacroscaleonlyaxis{\input{test.tikz.scaled}}
\def\mymacrolabelscaleonlyaxis{\input{test-label.tikz.scaled}}

\newlength{\test}
\begin{document}
    \mymacro%
    \settowidth{\test}{\mymacro}\quad\the\test

    \mymacrolabel%
    \settowidth{\test}{\mymacrolabel}\quad\the\test

    \mymacroscaleonlyaxis%
    \settowidth{\test}{\mymacroscaleonlyaxis}\quad\the\test

    \mymacrolabelscaleonlyaxis%
    \settowidth{\test}{\mymacrolabelscaleonlyaxis}\quad\the\test
\end{document}
erik
  • 12,673
  • Thanks a lot! It didn't work for me when I tried after your first comment, because I used tikzscale's overloaded \includegraphics. – bonanza Apr 26 '19 at 21:33