如何在标题/文本中绘制图例线而不包括原始情节的 TikZ 图片?

如何在标题/文本中绘制图例线而不包括原始情节的 TikZ 图片?

我想在文本/标题中引用一些情节线索。通过在相应的axis环境中为情节定义标签可以实现。但是,我正在使用外部化,只想分发 PDF 文件(而不是文件tikzpicture),但仍想在文本中引用情节线索。如何单独添加情节线索的标签而不必包含整个tikzpicture

作为一种解决方法,我添加了虚拟图并标记它们,但这会导致文档中出现一个点。任何隐藏它tikzpicture并占用零空间的方法也很有用。

\documentclass{article}
\usepackage{tikz}
\usepackage{pgfplots}
\begin{document}
This is how I reference to \ref{hwplot1} and \ref{hwplot2}. But the following 'tikzpicture' results in ploting a dot


%%% This is neccessary for drawing plot lines in the text
\begin{tikzpicture}
    \begin{axis}[hide axis]
        \addplot [
        color=red,
        solid,
        line width=0.9pt,
        forget plot
        ]
        (0,0);\label{hwplot1}
        \addplot [
        color=black,
        dashed,
        line width=1.2pt,
        forget plot
        ]
        (0,0);\label{hwplot2}
    \end{axis}
\end{tikzpicture}%
\end{document} 

答案1

\documentclass{article}
\usepackage{tikz}
\usepackage{pgfplots}
\begin{document}

\newcommand{\hwplotA}{\raisebox{2pt}{\tikz{\draw[red,solid,line width=0.9pt](0,0) -- (5mm,0);}}}
\newcommand{\hwplotB}{\raisebox{2pt}{\tikz{\draw[black,dashed,line width=1.2pt](0,0) -- (5mm,0);}}}

This is how I reference to {\hwplotA} and {\hwplotB}. 

\end{document} 

插入行


如有疑问,请使用保存箱。

\documentclass{article}
\usepackage{tikz}
\usepackage{pgfplots}
\begin{document}

\newcommand{\hwplotA}{\raisebox{2pt}{\tikz{\draw[red,solid,line width=0.9pt](0,0) -- (5mm,0);}}}
\newcommand{\hwplotB}{\raisebox{2pt}{\tikz{\draw[black,dashed,line width=1.2pt](0,0) -- (5mm,0);}}}

\begin{figure}[hp]
\sbox0{\hwplotA}\sbox1{\hwplotB}%
\caption{This is how I reference to \usebox0 and \usebox1.}
\end{figure}

\end{document} 

答案2

这不会消除tikz图片,但会将其缩放到零大小。在这里,它被放置在最后一个单词“nothing”的中间。

\documentclass{article}
\usepackage{tikz}
\usepackage{pgfplots}
\begin{document}
This is how I reference to \ref{hwplot1} and \ref{hwplot2}. But the following
  'tikzpicture' results in plotting no%
%%% This is neccessary for drawing plot lines in the text
\scalebox{0}{%
\begin{tikzpicture}
    \begin{axis}[hide axis]
        \addplot [
        color=red,
        solid,
        line width=0.9pt,
        forget plot
        ]
        (0,0);\label{hwplot1}
        \addplot [
        color=black,
        dashed,
        line width=1.2pt,
        forget plot
        ]
        (0,0);\label{hwplot2}
    \end{axis}
\end{tikzpicture}%
}%
thing
\end{document} 

在此处输入图片描述

答案3

为了供我和其他人在将来参考,我采用了 @steven-b-segletes 的出色示例,并将其推广为从循环列表中自动创建标签。这对于重复使用带有图例的标签非常有用,或者您正在使用动画包并且不想重复引用 300 个标签

\documentclass{article}
\usepackage{tikz}
\usepackage{pgfplots}
\begin{document}
\newcommand*{\graphlinewidth}{1}
\pgfplotscreateplotcyclelist{mylistNoMarkDarkColour}{%
  {red!80!black,    solid,      line width=\graphlinewidth, mark=none},
  {green!80!black,  solid,      line width=\graphlinewidth, mark=none},
  {blue,    solid,          line width=\graphlinewidth, mark=none},
  {purple,  solid,          line width=\graphlinewidth, mark=none},
  {orange,  solid,          line width=\graphlinewidth, mark=none},
}

% Generate the label references for legends in captions from the given cyle list and reference.
% Also avoids issues with TiKz externalize
% 1: Cycle list name, 2: Reference prefix, 3: Number to produce
\newcommand{\pgfplotsReferenceGenerator}[3]{%
\scalebox{0}{%
\begin{tikzpicture}
\begin{axis}[hide axis, cycle list name = #1]
  \foreach \i   [evaluate=\i] in {1,...,#2}{
    \addplot (0,0); \label{#3\i}
  }
\end{axis}
\end{tikzpicture}%
}%
}

\begin{document}

\pgfplotsReferenceGenerator{mylistNoMarkDarkColour}{5}{dark}
This is great for reusing labels where you have the legend in a caption,
or are using the animate package and dont want 300 repeated references (i.e one \ref{dark1} per frame)
like \ref{dark2} and \ref{dark3} with \ref{dark4} finally \ref{dark5}

\end{document}

我未能做到的事情以及希望得到以下方面的反馈:

  1. 自动检测循环列表的长度
  2. 正确禁用图表的 TiKz 外部化(我无法使其工作......) 在此处输入图片描述

相关内容