生成数十张图表

生成数十张图表

我正在修改微观经济学的旧讲义,其中包含很多图表。由于我正在用 LaTeX 重写它,我想重新创建旧图表,但我需要选择合适的工具来完成这项工作。这些图表通常涉及很多直线、双曲线、切线、交点、阴影区域、空间有限的标签……在大多数情况下,单个对象可以很容易地描述为数学函数,因此公式的解释是关键。

我可以在 GeoGebra 中绘制“足够好”的图表,但我正在寻找一种更灵活的解决方案来进行批量编辑,例如更改所有图表中的所有字体、颜色或粗细。如果我只编辑几行代码并重新渲染所有内容,那就太理想了。使用“非编码”图表进行批量编辑可能会很麻烦。

我对 TikZ/PGF 不太熟悉,所以不确定它是否合适。如果我有一个可以微调图表的工具,也会对我有很大帮助。使用 TikZ/PGF,我能想到的只是创建一个minimalLaTeX 文档并在保存时自动渲染。

它不必集成到 LaTeX 工作流程中,它可以是生成所有图形作为eps或的(命令行)脚本pdf

答案1

PGFplots 非常适合此用途。需要花一些时间来掌握 PGF/TikZ 和 PGFplots 语法,但一旦您找到了如何做某事的方法,就可以很容易地将其包装起来以style应用于其他问题。以下是您链接的示例,使用 PGFplots 绘制:

使用此代码:

\begin{tikzpicture}
\begin{axis}[
    ondrej's graphs,
]
\addplot {e^(-x)};
\addplot +[shift function={0.2}{0.2}] {e^(-x)} node [tangent point=0.3] {};
\addplot +[shift function={0.4}{0.4}] {e^(-x)};
\draw [tangent line];
\draw [point] (tangent) circle;
\pgfplotsset{project point on axes}
\end{axis}
\end{tikzpicture}

下一页的例子

可以使用生成

\begin{tikzpicture}
\begin{axis}[
    ondrej's graphs,
    xmin=0,xmax=4,
    ymin=0,ymax=4
]
\addplot +[sharp plot] coordinates {(0.5,3) (1,1) (3,0.5)};
\addplot +[sharp plot, shift function={0.4}{0.6}] coordinates {(0.5,3) (1,1) (3,0.5)};
\addplot +[sharp plot, shift function={0.8}{1.2}] coordinates {(0.5,3) (1,1) (3,0.5)};
\coordinate (tangent) at (axis cs:1.4,1.6);
\draw [point] (tangent) circle;
\draw [tangent line] (tangent) +(-40:4cm) -- +(-40:-4cm);
\pgfplotsset{project point on axes};
\node [label node] at (rel axis cs:0.03,0.92) {Indifference\\curves};
\node [label node] at (rel axis cs:0.53,0.23) {Budget line};
\end{axis}
\end{tikzpicture}

这些代码片段有点误导,因为它们使用了很多您必须自己编写的自定义样式。但是,它们表明,一旦您有了样式,创建类似的图表是多么容易。

以下是示例的完整代码:

\documentclass{standalone}
\usepackage{pgfplots}
\usetikzlibrary{decorations.markings}

\pgfplotsset{
    ondrej's graphs/.style={
        domain=0.2:4,
        xmin=0,xmax=5,
        ymin=0,
        axis lines*=left,
        xtick=\empty, ytick=\empty,
        xlabel=$x_1$,
        ylabel=$x_2$,
        every axis y label/.style={
            at={(current axis.above origin)},
            anchor=east
        },
        every axis x label/.style={
            at={(current axis.right of origin)},
            anchor=north
        },
        cycle list={
            ultra thick, orange, no markers, smooth\\
        },
        shift function/.style 2 args={
            x filter/.code={\pgfmathparse{\pgfmathresult+##1}},
            y filter/.code={\pgfmathparse{\pgfmathresult+##2}}
        },
        /tikz/tangent line/.style={
            ultra thick, black, shorten <=-4cm, shorten >=-4cm,
            insert path={(tangent.west) -- (tangent.east)}
        },
        /tikz/indicator lines/.style={
            thin, densely dashed
        },
        /tikz/point/.style={
            fill,
            radius=2.5pt,
        },
        project point on axes/.code={
            \pgfplotsset{/pgfplots/after end axis/.code={
                \draw [indicator lines]
                (tangent-|{rel axis cs:0,0}) 
                node [anchor=east] {$x_2^*$} 
                -| (tangent|-{rel axis cs:0,0})
                node [anchor=north] {$x_1^*$};
            }}
        },
        /tikz/label node/.style={
            font=\small,
            align=left,
            anchor=west
        }
    }
}

\tikzset{
        tangent point/.style={
            sloped,
            name=tangent,
            pos=#1
        },
        tangent point/.default=0.5
}

\begin{document}
%\begin{tikzpicture}
%\begin{axis}[
%   ondrej's graphs,
%]
%\addplot {e^(-x)};
%\addplot +[shift function={0.2}{0.2}] {e^(-x)} node [tangent point=0.3] {};
%\addplot +[shift function={0.4}{0.4}] {e^(-x)};
%\draw [tangent line];
%\draw [point] (tangent) circle;
%\pgfplotsset{project point on axes}
%\end{axis}
%\end{tikzpicture}

\begin{tikzpicture}
\begin{axis}[
    ondrej's graphs,
    xmin=0,xmax=4,
    ymin=0,ymax=4
]
\addplot +[sharp plot] coordinates {(0.5,3) (1,1) (3,0.5)};
\addplot +[sharp plot, shift function={0.4}{0.6}] coordinates {(0.5,3) (1,1) (3,0.5)};
\addplot +[sharp plot, shift function={0.8}{1.2}] coordinates {(0.5,3) (1,1) (3,0.5)};
\coordinate (tangent) at (axis cs:1.4,1.6);
\draw [point] (tangent) circle;
\draw [tangent line] (tangent) +(-40:4cm) -- +(-40:-4cm);
\pgfplotsset{project point on axes};
\node [label node] at (rel axis cs:0.03,0.92) {Indifference\\curves};
\node [label node] at (rel axis cs:0.53,0.23) {Budget line};
\end{axis}
\end{tikzpicture}
\end{document} 

答案2

我会推荐pgfplots为你的任务。它有很多功能,特别是允许您设置可以简化全局更改的样式。

完整的 MWE 位于本答案的末尾,但有一些亮点:

轴的样式

% axis style, ticks, etc
\pgfplotsset{every axis/.append style={
                    axis x line=middle,    % put the x axis in the middle
                    axis y line=middle,    % put the y axis in the middle
                    axis line style={<->}, % arrows on the axis
                    xlabel={$x$},          % default put x on x-axis
                    ylabel={$y$},          % default put y on y-axis
                    scale only axis,       % otherwise width won't be as intended: http://tex.stackexchange.com/questions/36297/pgfplots-how-can-i-scale-to-text-width
                    tick label style={font=\tiny},
                    label style={font=\small},
                    legend style={font=\tiny}}}

这将设置每个环境的默认值axis。当然,默认值可以被覆盖 - 例如,如果您想更改xlabelylabel,只需将它们作为参数提供即可 - 如下面 MWE 中所示。

塑造你的曲线

% line style
\pgfplotsset{myplot/.style={color=red,mark=none,line width=1pt,<->}} % this is pretty redundant in most cases now that cycle list is implemented

axis可以在以下环境中使用\addplot[myplot]...(请参阅下面的 MWE)

杂项样式 这些是箭头和网格的全局样式!

% arrow style
\tikzset{>=stealth}

% grid style
\pgfplotsset{grid style={dashed,gray}}

许多更多功能,但我将留给你去探索

平均能量损失

\documentclass{article}
\usepackage{pgfplots}

% cycle list- truly awesome; see section 4.6.7, pg 129 of pgfplots
\pgfplotscreateplotcyclelist{mystylelist}{%
color=red,mark=none,line width=1pt,<->\\%
color=blue,mark=none,line width=1pt,<->\\%
color=gray,mark=none,line width=1pt,<->\\%
}

% axis style, ticks, etc
\pgfplotsset{every axis/.append style={
                    axis x line=middle,    % put the x axis in the middle
                    axis y line=middle,    % put the y axis in the middle
                    axis line style={<->}, % arrows on the axis
                    xlabel={$x$},          % default put x on x-axis
                    ylabel={$y$},          % default put y on y-axis
                    scale only axis,       % otherwise width won't be as intended: http://tex.stackexchange.com/questions/36297/pgfplots-how-can-i-scale-to-text-width
                    cycle list name=mystylelist,
                    tick label style={font=\tiny},
                    label style={font=\small},
                    legend style={font=\tiny}}}
\tikzset{axisnode/.style={font=\tiny,text=black}}

% line style
\pgfplotsset{myplot/.style={color=red,mark=none,line width=1pt,<->}} % this is pretty redundant in most cases now that cycle list is implemented

% arrow style
\tikzset{>=stealth}

% grid style
\pgfplotsset{grid style={dashed,gray}}

\begin{document}

\begin{tikzpicture}
    \begin{axis}[xmin=-10,xmax=10,
                 ymin=-10,ymax=10,
                 width=.5\textwidth]
        \addplot expression[domain=-10:10]{x};
        \legend{My function}
    \end{axis}
\end{tikzpicture}

% Use \addplot+ if you want to inherit some features
% of the cyclelist style, but overwrite others
\begin{tikzpicture}
    \begin{axis}[xmin=-10,xmax=10,
                 ymin=-10,ymax=10]
        \addplot+[->] expression[domain=-10:10]{x};
    \end{axis}
\end{tikzpicture}

% you can use the myplot style as follows
\begin{tikzpicture}
    \begin{axis}[xmin=-10,xmax=10,
                 ymin=-10,ymax=10]
        \addplot[myplot] expression[domain=-10:10]{x};
    \end{axis}
\end{tikzpicture}
\end{document}

相关内容