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'm trying to put together some diagrams in LaTeX.

My dilemma is as follows. I have a technical report that contains a lot of images from MATLAB. I need to be able to freehand / draw other plots (such as wavelet functions):

wavelet functions

So I have a couple of options:

  • Use PowerPoint for all diagrams (the authoring tool I'm most familiar with). The problem is everything looks out of place / mismatched with stuff coming from MATLAB + PowerPoint
  • Use MATLAB for everything. Generating diagrams that illustrate my points the way I would like to can be a bit tedious but it can be done.
  • Use SOME KIND OF LaTeX diagramming toolkit. I currently don't know anything about LaTeX diagramming, but I wouldn't mind exploring. It seems tedious. Are there any LaTeX diagramming toolkits that have a GUI?

As an experienced LaTeX user, what is your guide to generating professional math graphs that don't look out of place and don't take decades to complete?

share|improve this question
6  
I (and most here) would recommend using tikz and pgfplots for this sort of thing. It's very powerful and once you get the hang of it, it's quite easy to use. If you browse through the tags here on the site you'll find plenty of examples to learn from. –  Alan Munn Nov 20 '11 at 22:15
4  
matlab2tikz, a script to generate TikZ/PGFplots files from Matlab figures may also be useful. –  Torbjørn T. Nov 20 '11 at 22:18
    
Another option would be to use gnuplot. It has several output formats (called terminals) that can be used with LaTeX. –  Marco Nov 20 '11 at 22:26

3 Answers 3

up vote 15 down vote accepted

For tasks like this you can do everything with coordinates. As mentioned in the comments of your question, the package to use is pgfplots. It's text based, but very easy to use. I think it's actually better than Matlab for getting the plot how you want quickly. For example, your first plot would be (updated, thanks Jake!)

\documentclass[10pt]{article}
\usepackage{pgfplots}
\begin{document}

\pgfplotsset{width=6cm,compat=newest}

\begin{figure}[ht]
\begin{tikzpicture}
\begin{axis}[
    scale only axis,
    xlabel={$x$-axis},
    ylabel={$y$-axis},
    ymin=-1.5,
    ymax=1.5,
    xmin=-0.5,
    xmax=1.5,
]
\addplot[color=blue,thick] coordinates {
            (0, 0)
            (0, 1)
            (1, 1)
            (1, 0)
        };
% The following command puts the line in on the axis.
\draw ({rel axis cs:0,0} |- {axis cs:0,0}) -- ({rel axis cs:1,0} |- {axis cs:0,0});
\end{axis}
\end{tikzpicture}
\end{figure}

\end{document}

which gives you this:

Simple plot with coordinates

Now that may look like a lot of code, but most of it is reusable. The axes labels are obvious, and the ranges too. This should be pretty clear if you're used to plotting in matlab. The scale only axis is to make the size of the pgfplot axes 6cm rather than the entire plot with labels.

Things are added to the canvas with \addplot. In this case I put in coordinates directly; perfect for your sketches. You can put in a file instead though. If your file contains columns of data with labels at the top. For example, this code

\pgfplotsset{width=6cm,compat=newest}
\begin{axis}[
    scale only axis,
    xlabel={$\tau$ ($\mu$s)},
    ylabel={P(bitflip)},
    ymin=0,
    ymax=1,
    xmin=0,
    xmax=125.66,
]
\addplot[thick,color=red]
    table[x=time,y=fidelity] {data.txt};
\end{axis}
\end{tikzpicture}

with a data file data.txt that looks like this (tab delimited and easy to produce with Matlab)

time    fidelity
0.00000000e+00  0.00000000e+00
2.16135894e-01  1.11612759e-04
7.52429193e-01  8.16588122e-04
1.29606053e+00  8.76288172e-04
1.84266188e+00  7.06435551e-04
2.40147636e+00  7.93280613e-04
2.95870895e+00  6.89534671e-04
3.50977092e+00  8.62614471e-04

produces this

enter image description here

The most notable benefits of this approach is that if you're doing lots of similar plots you need only alter a few lines of coordinates, and that the typeface matches in size and style with the rest of your document. Another benefit is that the size of the plot is very easy to control, which I always found difficult with Matlab. Finally, if something changes, like you decide to use a different colour or correct a label, then it's a quick edit, rather than regenerating a whole graphic by opening up an external package.

share|improve this answer
1  
Yup, very nice! For drawing the line at y=0, I would suggest using \draw ({rel axis cs:0,0} |- {axis cs:0,0}) -- ({rel axis cs:1,0} |- {axis cs:0,0});, which will draw a line from the left edge of the plot to the right edge, regardless of the axis limits. The expression looks a bit intimidating at first, but it's not too complicated: rel axis cs:0,0 is the lower left corner of the plot, axis cs:0,0 is the origin of the data coordinate system, (A|-B) is the point at the intersection of a vertical line through A and a horizontal line through B. –  Jake Nov 21 '11 at 2:24
    
Yup, that's true! I totally forgot, so I'll update the plot. Thanks! –  qubyte Nov 21 '11 at 2:30

The preferred diagramming tools in TeX are TikZ, PSTricks and MetaPost, all of which are text based and have a high learning curve. If you are looking at a easy to use GUI tool, have a look at Ipe. This PracTeX article gives a nice summary of the features and usage of Ipe.

share|improve this answer

Why not use pst-plot? It is one of PSTricks packages that is designed specifically for plotting almost anything with minimal effort. Visit PSTricks galleries and look around for pst-plot examples.

Example:

\documentclass{article}
\usepackage{pst-plot}
\begin{document}
\begin{pspicture}(-1,-1)(6,1)
\psaxes{->}(0,0)(-0.75,-0.75)(5.75,0.75)[$x$,90][$y$,180]
\psline[linecolor=blue](1,0)(1,0.5)(5,0.5)(5,0)
\end{pspicture}
\end{document}

For simplicity, compile it with xelatex. For faster compilation, use latex-dvips-ps2pdf sequence.

See the output here.

share|improve this answer

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.