Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

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.

share|improve this question
4  
I'd use PGFplots package for that, it's definitely up to all the tasks you mentioned (although I am not sure what "space-limited labeling" is). It's not interactive, so you'd have to go with recompiling the standalone document (standalone documents are automatically cropped to the size of the content. minimal is not a document class that should be used for producing anything but test documents for bug hunting). It's perfect for getting elegant graphs with a homogeneous appearance. –  Jake Jun 19 '12 at 13:45
1  
Another option would be to draw graphs in R and export either as PDF, EPS, or TikZ code. –  Seamus Jun 19 '12 at 13:47
2  
you also use gnuplot with tikz texample.net/tikz/examples/tag/gnuplot –  rpapa Jun 19 '12 at 14:22
    
@Seamus, I like the idea, but what about the labels within the plot? I feel like PGF has an edge in this regard, but is a bit more cumbersome to use. @Jake, I'll give it a shot, thanks for the standalone tip. Space-limited labeling was a stupid way of saying there are sometimes a lot of curves and it's necessary to fit labels manually (e.g. using coordinates) as automatic placement may fail. –  Ondrej Jun 19 '12 at 14:26
1  
Just realized this is pretty much what I am aiming for: people.ischool.berkeley.edu/~hal/Books/im-outline.pdf (for example page 21 contains a nice figure - curves, lines, labels, dotted lines, ...) –  Ondrej Jun 19 '12 at 14:44
show 1 more comment

2 Answers

I would recommend pgfplots for your task. It has so many features, and in particular, allows you to set styles that can ease global changes.

A complete MWE is at the end of this answer, but some highlights:

Styling the axis

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

This sets the default of each axis environment. Of course, the default can be overwritten- for example, if you want to change the xlabel or ylabel, you simply provide them as an argument- demonstrated below in the MWE.

Styling your curves

% 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

Can be used in an axis environment as \addplot[myplot]... (see MWE below)

Miscellaneous styles These style the arrows and grid- globally!

% arrow style
\tikzset{>=stealth}

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

There are many more features, but I'll leave you to explore them

MWE

\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}
share|improve this answer
add comment

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

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,
            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} 
share|improve this answer
    
I recently asked a related question (tex.stackexchange.com/questions/65578/…) on plot styles etc. SO now you have defined a graph style, do you have to include it in every preamble or can you save it as a tex/pgf/txt file and then just call it? ie have the \pgfplotsset... saved in an external file as ondrej_graph.tex and then pgfplots will look for it when \begin{axis}[ondrej_graph... is called? –  Leeser Aug 8 '12 at 10:56
1  
@Leeser: You could save the \pgfplotsset options for your various styles in some mystyles.tex file and input that using \input{mystyles}. That would make all the styles available. Is that what you mean? –  Jake Aug 8 '12 at 11:04
    
exactly what I meant. Now I know where to start with the above examples etc I can start answering my own question above...Thanks very much. You, percusse, andrewstacey,Josephwright, ChristianFeuersaenger, are owed a depth of gratitude from us novices... –  Leeser Aug 8 '12 at 11:53
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.