如何在 LaTeX 中创建具有特定曲线和标记轴的图?

如何在 LaTeX 中创建具有特定曲线和标记轴的图?

我正在尝试在 LaTeX 中创建一个类似于附图的图。有人能帮我提供在 LaTeX 中创建这种曲线的代码吗?

谢谢。

在此处输入图片描述

答案1

这是一种可能的方法元帖子。我希望我的绘图足够简单,可以作为教程示例,但您可能还想点击上面的链接查看手册和一些介绍材料。

在此处输入图片描述

它被包装在luamplib包中,因此您需要使用它来编译它lualatex

\documentclass[border=5mm]{standalone}
\usepackage{luamplib}
\usepackage{fontspec}
\setmainfont{TeX Gyre Heros}
\begin{document}
\mplibtextextlabel{enable}
\mplibnumbersystem{decimal}
\begin{mplibcode}
beginfig(1);
    numeric period, rate, present_value, future_value;
    period = 20; % years
    rate = 15; % percent
    present_value = 1;
    future_value = present_value * (1+rate/100) ** period;

    numeric u, v;
    5u = 8v = 3.5cm;  % set horizontal and vertical scale

    % curve 
    path ff;
    ff = ((0, present_value) for years = 1 upto period:
        .. (years, present_value * (1+rate/100) ** years)
    endfor) xscaled u yscaled v;
    draw ff withpen pencircle scaled 1 withcolor 2/3 red;

    % axes
    path xx, yy;
    xx = origin -- (period, 0) scaled u;
    yy = origin -- (0, ceiling future_value) scaled v;
    draw xx; 
    draw yy;

    % scales
    for x = 0 step 5 until period: 
        draw (origin -- 2 down) shifted (x * u, 0);
        label.bot(decimal x, (x * u, -2));
    endfor
    for y = 0 step 2 until ceiling future_value:
        draw (origin -- 2 left) shifted (0, y * v);
        label.lft(decimal y, (-2, y*v));
    endfor

    % axis labels
    label("Years", (1/2 period * u, -24));
    label(textext("Euros") rotated 90, (-24, 1/2 future_value * v));

    % start and finish labels
    label("\begin{tabular}{c} Present value\\" 
          & decimal present_value 
          & " euro\end{tabular}", (1.8u, 2.7v));
    label("\begin{tabular}{c} Future value\\" 
          & decimal (floor(10 * future_value + 1/2) / 10)
          & " euros\end{tabular}", ((period - 2.2) * u, future_value * v));

    % arrow annotations
    path cap, dis;
    cap = subpath (9, 13) of ff shifted (8 * unitvector(direction 11 of ff rotated 90));
    dis = subpath (12, 8) of ff shifted (8 * unitvector(direction 10 of ff rotated -90));
    drawarrow cap; label.ulft("\small Capitalisation", point 2 of cap);
    drawarrow dis; label.lrt("\small Discounting", point 2 of dis);

    % title
    label.top("\large\textbf{Discounting and capitalisation at " & decimal rate & "\%}", 
        point 5/2 of bbox currentpicture shifted 8 up);

endfig;
\end{mplibcode}
\end{document}

如果你没有TeX Gyre Heros安装,请选择其他字体,或从这里

相关内容