如何标记 TiKz 形状以便在标题中引用其图例

如何标记 TiKz 形状以便在标题中引用其图例

如何标记 TiKz 线条样式,以便在图题中进一步参考。因为\addplot标签只是跟在它后面。但是,我不知道该怎么做\draw

梅威瑟:

\documentclass{book}
\usepackage{lipsum}
\usepackage{caption}
\usepackage{pgfplots}
\pgfplotsset{compat=1.3}
\tikzset{pointille/.style={dash pattern = on 2pt off 2pt on 6pt off 2pt}}
\tikzset{points/.style={dash pattern = on 1pt off 1pt}}
\tikzset{tirets/.style={dash pattern = on 5pt off 5pt}}

\begin{document}
\lipsum[1]
\begin{figure}[!ht]
\centering
\captionsetup{width=9.5cm}
\pgfplotsset{every axis plot post/.append style={mark=none,line width=1.5pt}}
\begin{tikzpicture}
\begin{axis}[height=7cm,width=9cm,grid=major,xlabel=$x$,ylabel=$f(x)$,
tick label style={font=\footnotesize},label style={font=\small},max space between ticks=45, major tick length=0cm,minor tick length=0cm,enlargelimits=false,]
\addplot[tirets,color=blue]{2*x};\label{p4}
\addplot[pointille,color=green]{0.5*x*x};\label{p5}
\addplot[points,color=red]{-0.125*x*x*x};\label{p6}
\end{axis}

\draw [dashed,red,thick] (1,1) rectangle (2,2); \label{p7}
\draw [solid,blue,thick] (1,1)-- (2,2); \label{p8}
\end{tikzpicture}
\caption[Caption in ToC]{This is a plot about colored curves: $f(x)=2 x$ (\ref{p4}),  $f(x)=0.5 x^2$ (\ref{p5}), and $f(x)=-0.125 x^3$ (\ref{p6}), (\ref{p7}), and (\ref{p8})}
\end{figure}
\lipsum[1]
\end{document}

在此处输入图片描述

答案1

\label/\ref功能由 PGFPlots(而非 TikZ)提供,并且仅适用于绘图,而不适用于任意\draw命令。

\addplot您可以使用绘制对象的命令来实现所需的结果:您可以\draw [dashed,red,thick] (1,1) rectangle (2,2);用 替换\addplot [dashed,red,thick] coordinates {(1,1)} rectangle (2,2);。您只需要在列表中提供路径的第一个坐标coordinates,之后您可以使用常规的 TikZ 路径命令,如rectangle--。请注意,坐标系在环境内缩放axis,因此最好的选择是使用(axis cs:<x>,<y>)坐标,而不是尝试使用“正常”坐标找到正确的值。

\documentclass{book}
\usepackage{lipsum}
\usepackage{caption}
\usepackage{pgfplots}
\pgfplotsset{compat=1.3}
\tikzset{pointille/.style={dash pattern = on 2pt off 2pt on 6pt off 2pt}}
\tikzset{points/.style={dash pattern = on 1pt off 1pt}}
\tikzset{tirets/.style={dash pattern = on 5pt off 5pt}}

\begin{document}
\lipsum[1]
\begin{figure}[!ht]
\centering
\captionsetup{width=9.5cm}
\pgfplotsset{every axis plot post/.append style={mark=none,line width=1.5pt}}
\begin{tikzpicture}
\begin{axis}[height=7cm,width=9cm,grid=major,xlabel=$x$,ylabel=$f(x)$,
tick label style={font=\footnotesize},label style={font=\small},max space between ticks=45, major tick length=0cm,minor tick length=0cm,enlargelimits=false,]
\addplot[tirets,color=blue]{2*x};\label{p4}
\addplot[pointille,color=green]{0.5*x*x};\label{p5}
\addplot[points,color=red]{-0.125*x*x*x};\label{p6}
\addplot [dashed,red,thick] coordinates {(-4,-10)} rectangle (axis cs:-2,-3); \label{p7}
\addplot [solid,blue,thick] coordinates {(-4, -10)} -- (axis cs:-2,-3); \label{p8}
\end{axis}

\end{tikzpicture}
\caption[Caption in ToC]{This is a plot about colored curves: $f(x)=2 x$ (\ref{p4}),  $f(x)=0.5 x^2$ (\ref{p5}), and $f(x)=-0.125 x^3$ (\ref{p6}), (\ref{p7}), and (\ref{p8})}
\end{figure}
\lipsum[1]
\end{document}

相关内容