pgfplots:如果没有给出标签,则设置图形的有效宽度

pgfplots:如果没有给出标签,则设置图形的有效宽度

pgfplot如果我没有标签,如何确保正确应用图形的设置宽度?

请考虑下面的例子

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

设置了 ylabel 的情况,缩放比例几乎正确(约 118pt,而不是所需的 120pt)。但是,在这两种情况下,没有标签的图形都无法正确缩放。如何解决?

在此处输入图片描述

答案1

您可以将trim axis left(应用于tikzpicture) 与scale only axis(应用于axis) 组合使用。如果您仅修剪轴,pgfplots仍将为刻度标记和轴标签分配 45pt 的空间。如果您仅缩放轴,轴装饰将增加整体宽度。

在此处输入图片描述

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

相关内容