# [Tex/LaTex] Generating dozens of graphs

graphicsgraphstikz-pgf

I am revising old lecture notes on microeconomics and they contain a lot of graphs. As I am rewriting it in LaTeX, I would like to recreate the old graphs, but I need to select the right tool for the job. The graphs generally involve a lot of lines, hyperbolas, tangents, intersections, shaded regions, space-limited labeling, … Individual objects can, in most cases, be easily described as mathematical functions, so interpretation of formulae is key.

I could just draw "good enough" graphs in GeoGebra, but I am looking for a more flexible solution for mass edits, e.g. changing all fonts in all graphs, colors, or thickness. It would be ideal if I'd just edit a few lines of code and rerender everything. Mass editing with "non-coded" graphs would probably be a pain.

I am not that familiar with TikZ/PGF, so I am not sure if it fits. It would also help me greatly if I had a tool where I could fine tune my graphs. With TikZ/PGF, I can think of only creating a minimal LaTeX document and automatic rendering on save.

It does not have to be integrated into a LaTeX workflow, it can be a (command line) script that generates all graphs as eps or pdf.

PGFplots would be suited quite well for this. It takes a bit of time to get hang of the PGF/TikZ and the PGFplots syntax, but once you've found out how to do something, it's really easy to wrap it in a style to apply it to other problems. Here's the example you linked to, drawn using PGFplots: using this code:

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


And the example from the next page can be generated using

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


These code snippets are a bit misleading, since they use a lot of custom styles that you have to write yourself. However, they show how easy it is to create similar plots once you have the styles.

Here's the complete code for the examples:

\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,
},
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 +[shift function={0.2}{0.2}] {e^(-x)} node [tangent point=0.3] {};
%\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}