避免在 xticklabels 中重复 \num / 其他命令

避免在 xticklabels 中重复 \num / 其他命令

我创建了以下图像,对此我非常满意

在此处输入图片描述

由于不断遇到溢出错误,我不得不缩减函数。我对代码很满意,但

   yticklabels = {0,\num{20000}, \num{40000}, \num{60000}, \num{80000}, \num{100000},%
                   \num{120000},\num{140000},\num{160000},\num{180000},\num{200000}},

部分。这能以某种方式实现自动化吗,也许使用for-loop

\documentclass[tikz]{standalone}

\definecolor{maincolorLight}{HTML}{838fbc}
\definecolor{maincolorMedium}{HTML}{425b9b}
\definecolor{maincolorDark}{HTML}{003d80}

\usepackage{pgfplots, siunitx}

\pgfplotsset{
    Eksamen1/.style={
        scale only axis,
        grid=major,
        grid style={dashed, gray!50},
        axis lines=middle,
        inner axis line style={=>},
        xlabel={\large $x$},
        ylabel={\large $y$},
        yticklabel style={inner ysep=0pt, anchor=south east},
        xticklabel style={inner xsep=0pt, anchor=north west},
            after end axis/.code={
                \path (axis cs:0,0) 
                    node [anchor=north west,yshift=-0.075cm] {0}
                    node [anchor=south east,xshift=-0.075cm] {0};
            }
    },
}

\begin{document}

    \begin{tikzpicture}
        \begin{axis}[
            Eksamen1,
            ytick={0,2,...,20},
            yticklabels = {0,\num{20000}, \num{40000}, \num{60000}, \num{80000}, \num{100000},%
                            \num{120000},\num{140000},\num{160000},\num{180000},\num{200000}},
            xtick={0,1,...,8},
            ymin=-.1,
            ymax=23,
            xmin=-.1,
            xmax=9,
            domain = 0:8,
        ]
            \addplot[color=maincolorMedium!30!white,thick,samples=50] {20-2*x} node[above,pos=1] {$g$};
            \addplot[color=maincolorMedium,thick,samples=50] {20*e^(-0.2*x)} node[below,pos=0.5] {$h$};
            \addplot[color=maincolorMedium!30!black,thick,samples=50] {20*0.9^x} node[above,pos=0.6] {$f$};
            \node[] at (axis cs: 2.5,21) {bilens verdi (kroner)};
            \node[] at (axis cs: 7,1.5) {\AA r etter 2017};
        \end{axis}
    \end{tikzpicture}

\end{document}

答案1

添加\usepackage{xfp}并使用yticklabel = {\num{\fpeval{\tick*10000}}}

\documentclass[tikz]{standalone}
\usepackage{xfp}

\definecolor{maincolorLight}{HTML}{838fbc}
\definecolor{maincolorMedium}{HTML}{425b9b}
\definecolor{maincolorDark}{HTML}{003d80}

\usepackage{pgfplots, siunitx}

\pgfplotsset{
    Eksamen1/.style={
        scale only axis,
        grid=major,
        grid style={dashed, gray!50},
        axis lines=middle,
        inner axis line style={=>},
        xlabel={\large $x$},
        ylabel={\large $y$},
        yticklabel style={inner ysep=0pt, anchor=south east},
        xticklabel style={inner xsep=0pt, anchor=north west},
            after end axis/.code={
                \path (axis cs:0,0) 
                    node [anchor=north west,yshift=-0.075cm] {0}
                    node [anchor=south east,xshift=-0.075cm] {0};
            }
    },
}

\begin{document}

    \begin{tikzpicture}
        \begin{axis}[
            Eksamen1,
            ytick={0,2,...,20},
            yticklabel = {\num{\fpeval{\tick*10000}}},
            xtick={0,1,...,8},
            ymin=-.1,
            ymax=23,
            xmin=-.1,
            xmax=9,
            domain = 0:8,
        ]
            \addplot[color=maincolorMedium!30!white,thick,samples=50] {20-2*x} node[above,pos=1] {$g$};
            \addplot[color=maincolorMedium,thick,samples=50] {20*e^(-0.2*x)} node[below,pos=0.5] {$h$};
            \addplot[color=maincolorMedium!30!black,thick,samples=50] {20*0.9^x} node[above,pos=0.6] {$f$};
            \node[] at (axis cs: 2.5,21) {bilens verdi (kroner)};
            \node[] at (axis cs: 7,1.5) {\AA r etter 2017};
        \end{axis}
    \end{tikzpicture}


\end{document}

相关内容