是否可以将案例与 pgfplots 图例结合起来?

是否可以将案例与 pgfplots 图例结合起来?

我想打印一个函数并添加一个利用的图例cases

当前代码是:

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

其结果为:

狄利克雷函数

我希望得到这样的结果:

在此处输入图片描述

(使用 gimp 和这个 LaTeX 片制作:

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

是否可以直接在 LaTeX 中制作这样的图例?

答案1

以下是一种可能性:

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

结果:

在此处输入图片描述

为了实现更好的垂直对齐,请考虑更改:

\tikz[baseline=-0.5ex]

进入:

\tikz[baseline=-\mathvalign]

哪里\mathvalign

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

按照在对齐或聚集环境中使用 tikzpicture 矩阵

相关内容