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 have the following plot

enter image description here

with this latex-source:

\documentclass{minimal}
\usepackage{tikz}
\usetikzlibrary{plotmarks}

%%%<
\usepackage{verbatim}
\usepackage[active,tightpage]{preview}
\PreviewEnvironment{tikzpicture}
\setlength\PreviewBorder{5pt}%
%%%>

% The data files, written on the first run.
\begin{filecontents}{function.data}
# n     m
1 1
1 2
2 1
1 3
2 2
3 1
1 4
2 3
3 2
4 1
1 5
2 4
3 3
4 2
5 1
\end{filecontents}

\begin{document}
\begin{tikzpicture}[y=.5cm, x=.5cm,font=\sffamily]
    %axis
    \draw (0,0) -- coordinate (x axis mid) (5,0);
        \draw (0,0) -- coordinate (y axis mid) (0,5);
        %ticks
        \foreach \x in {0,...,5}
            \draw (\x,1pt) -- (\x,-3pt)
            node[anchor=north] {\x};
        \foreach \y in {0,...,5}
            \draw (1pt,\y) -- (-3pt,\y) 
                node[anchor=east] {\y}; 
    %labels      
    \node[below=0.8cm] at (x axis mid) {n};
    \node[rotate=90, above=0.8cm] at (y axis mid) {m};

    %plots
    \draw plot[mark=square*]
        file {function.data};  

    %legend
    \begin{scope}[shift={(4,4)}] 
    \draw (0,0) -- 
        plot[mark=square*, mark options={fill=black}] (0.25,0) -- (0.5,0)
        node[right]{f(m, n)};
    \end{scope}
\end{tikzpicture}
\end{document}

Now I would like to place numbers into the marks or replace the marks by numbers. How is this possible?

share|improve this question
    
Can you be more specific? What numbers do you want to place? –  Turion Mar 26 '12 at 18:38
    
related: tex.stackexchange.com/questions/39635/… –  Marco Daniel Mar 26 '12 at 19:36

1 Answer 1

up vote 9 down vote accepted

I would use PGFplots for this, instead of building the plots by hand. In PGFplots, you can use the nodes near coords key for generating labels at each data point.

Here's your plot reproduced in PGFplots, with the labels.

\documentclass{article}
\usepackage{pgfplots}
\usepackage{sansmath} % for sans serif math

%%%<
% The data files, written on the first run.
\begin{filecontents}{function.data}
# n     m
1 1
1 2
2 1
1 3
2 2
3 1
1 4
2 3
3 2
4 1
1 5
2 4
3 3
4 2
5 1
\end{filecontents}

\begin{document}

\begin{tikzpicture}
\begin{axis}[
    compat=newest, % for better label placement
    font=\sansmath\sffamily, % math and normal text in sans serif
    xlabel=n, ylabel=m, % the label texts
    xmin=0, ymin=0, % axis origin
    enlarge y limits=false, % don't enlarge the y axis beyond the data range
    enlarge x limits={upper,abs=0.02}, % enlarge x axis slightly to make sure the last tick mark is drawn completely
    axis lines*=left, % only draw the left axis lines, not a box
    unit vector ratio*={1 1 1}, % equal axis scaling. "*" to make sure the axes can only be reduced in size, not enlarged
    width=6cm, % set the overall width of the plot
    try min ticks=5, % adjusts how many ticks are printed
    tick align=center, % tick marks centered on the axes
    legend style={
        draw=none, % no frame around axes
        at={(1,1)}, % place at upper right of plot
        anchor=north % use upper middle edge of legend for alignment
    },
]
\addplot [
    mark=square*, mark size=0.5em, % square, filled ("*"), radius of 0.5em
    nodes near coords={
        \pgfmathparse{int(\coordindex+1)}
        \pgfmathresult
    }, % print labels on each data point, using `\coordindex` (the data point counter) increased by 1
    every node near coord/.style={
        font=\scriptsize\sffamily\bfseries, % smaller text size, bold for the data point labels
        text=white,
        anchor=center % center the labels on the plot marks
    }
    ] table {function.data};
\addlegendentry{f(m,n)}
\end{axis}
\end{tikzpicture}
\end{document}
share|improve this answer
    
Thank you very much, this is exactly what I was looking for. But when I compile your code, I get ! LaTeX Error: File standalone.cls' not found.. How can I fix that? If I replace standalone` with article I get [Loading MPS to PDF converter (version 2006.09.02).] ) ! Missing number, treated as zero. <to be read again> u l.57 \end{axis} . –  moose Mar 27 '12 at 8:32
    
standalone isn't necessary, it's just a class that adjusts the paper size to the size of the content. The error message with the article class is strange. What version of PGF/TikZ and PGFplots are you using? –  Jake Mar 27 '12 at 8:43
    
I am using texlive-full on Ubuntu 10.04.4 LTS. I think it is version 1.2.2. –  moose Mar 27 '12 at 10:28
1  
Ah, yes, that is a problem. The packages that ship with Ubuntu are hopelessly outdated, especially with TikZ you will be much much happier if you install the normal current versions. I use Ubuntu with a "Vanilla" install of TeXLive, as described in tex.stackexchange.com/questions/1092/…, which works great. –  Jake Mar 27 '12 at 10:32
1  
\coordindex isn't an actual TeX counter, but merely a macro containing the value as a string. You can use it in mathematical expressions using \pgfmathparse{<expression>}, which is a very powerful and convenient maths engine. The result of your expression will be stored in the macro \pgfmathresult. I've edited the code to show how to increase the counter. –  Jake Mar 28 '12 at 10:24

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.