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 would like to print a function and add a legend which makes use of cases.

The current code is:

\documentclass{article}
\usepackage[pdftex,active,tightpage]{preview}
\setlength\PreviewBorder{2mm}

\usepackage{amssymb,amsmath}
\usepackage{pgfplots}

\begin{document}
\begin{preview}
\begin{tikzpicture}
    \begin{axis}[
        axis lines=middle,
        width=15cm, height=15cm,     % size of the image
        grid = both,
        grid style={dashed, gray!30},
        enlargelimits=true,
        xmin=-1,     % start the diagram at this x-coordinate
        xmax= 1,     % end   the diagram at this x-coordinate
        ymin= 0,     % start the diagram at this y-coordinate
        ymax= 1,     % end   the diagram at this y-coordinate
        /pgfplots/xtick={-1,-0.8,...,1}, % make steps of length 0.2
        /pgfplots/ytick={0,0.1,...,1},   % make steps of length 0.1
        axis background/.style={fill=white},
        ylabel=y,
        xlabel=x,
        legend style={at={(0.8,0.8)}, anchor=north}]
      \addplot[domain=-1:1, ultra thick,samples=100,red]  {1}; 
      \addplot[domain=-1:1, ultra thick,samples=100,blue] {0}; 
      \legend{$1 \text{, if } x \in \mathbb{Q}$, $0 \text{, if } x \in \mathbb{R} \setminus \mathbb{Q}$}
    \end{axis} 
\end{tikzpicture}
\end{preview}
\end{document}

Which produces:

dirichlet function

and I would like to get something like this:

enter image description here

(made with gimp and this LaTeX-piece:

$f(x) = 
    \begin{cases}
        1 & \text{if } x \in \mathbb{Q}\\
        0 & \text{if } x \in \mathbb{R} \setminus \mathbb{Q}
    \end{cases}$

)

Is it possible to make a legend like this directly in LaTeX?

share|improve this question
add comment

1 Answer 1

up vote 8 down vote accepted

Here is one possibility:

\documentclass{article}
\usepackage[pdftex,active,tightpage]{preview}
\setlength\PreviewBorder{2mm}

\usepackage{amssymb,amsmath}
\usepackage{pgfplots}
\pgfplotsset{compat=1.6}

\begin{document}
\begin{preview}
\begin{tikzpicture}
    \begin{axis}[
        axis lines=middle,
        width=15cm, height=15cm,     % size of the image
        grid = both,
        grid style={dashed, gray!30},
        enlargelimits=true,
        xmin=-1,     % start the diagram at this x-coordinate
        xmax= 1,     % end   the diagram at this x-coordinate
        ymin= 0,     % start the diagram at this y-coordinate
        ymax= 1,     % end   the diagram at this y-coordinate
        /pgfplots/xtick={-1,-0.8,...,1}, % make steps of length 0.2
        /pgfplots/ytick={0,0.1,...,1},   % make steps of length 0.1
        axis background/.style={fill=white},
        ylabel=y,
        xlabel=x,]
      \addplot[domain=-1:1, ultra thick,samples=100,red]  {1};
      \label{plot one}
      \addplot[domain=-1:1, ultra thick,samples=100,blue] {0};
      \label{plot two} 
      \node [draw,fill=white] at (rel axis cs: 0.8,0.8) {\shortstack[l]{
      $f(x) = 
    \left\lbrace\begin{array}{@{}l@{}l@{}l@{}}
       \tikz[baseline=-0.5ex]\node{\ref{plot one}}; \phantom{1cm}& 1 &  \text{ if } x \in \mathbb{Q}\\
       \tikz[baseline=-0.5ex]\node{\ref{plot two}}; & 0 & \text{ if } x \in \mathbb{R} \setminus \mathbb{Q}
    \end{array}\right.
$}};
    \end{axis} 
\end{tikzpicture}
\end{preview}
\end{document}

The result:

enter image description here

In order to achieve a better vertical alignment, consider to change:

\tikz[baseline=-0.5ex]

into:

\tikz[baseline=-\mathvalign]

where \mathvalign is:

\def\mathvalign{\the\dimexpr\fontdimen22\textfont2\relax}

as per use of tikzpicture matrix in align or gather environment.

share|improve this answer
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.