梅威瑟:

梅威瑟:

我正在寻找一种方法,从普通文本或标题中引用TikZ我自己标记的图形部分(a)(b)等等。对于subcaption多个单独的图形,我会使用类似于\subref{label}标题和\ref{label}普通文本的方法。但是当我手动将标签放在图形上时,我还没有找到类似的方法。

我已经找到了一种实现此目的的方法,但它不适用于外部图形。另外,我认为这种方法太麻烦了,我希望找到一种替代方法。

我基本上用的是马丁的答案这里\hypertarget,其工作原理是在环境中的自定义标签上使用TikZ,然后使用从其他地方链接到它\hyperlink

例如,我可以从正文中引用整个图形及其三个标记部分,如下所示:

Overall figure in Fig.~\ref{fig:figure}. Also see Fig.~\hyperlink{subfig:a}{1a}, Fig.~\hyperlink{subfig:b}{1b} and Fig.~\hyperlink{subfig:c}{1c}.

或者像标题中的那样(不确定为什么\protect需要):

\caption{\protect\hyperlink{subfig:a}{\subcaptiontext{a}} shows this; \protect\hyperlink{subfig:b}{\subcaptiontext{b}} shows that; and \protect\hyperlink{subfig:c}{\subcaptiontext{c}} this and that.}

输出

但这并不方便,因为我必须手动输入图号和标签才能链接到感兴趣的部分,这违背了整个目的。此外,这适用于非外部化图形,但启用外部化后超链接指向任何地方。

我也尝试过使用subcaption\phantomcaption但无法正常工作。也许有一个简单的解决方案,那就是使用subcaption

我很高兴看到解决方案可以修复我当前的方法或建议一种不太麻烦的替代方法。我更喜欢后者。

梅威瑟:

\documentclass{article}

\newcommand{\figFilename}{} % Store the figure filename for convenience.
\newcommand{\subcaptiontext}[1]{\textsf{\textbf{(#1)}}} % Custom label format.

\usepackage{filecontents}
\begin{filecontents*}{figure.tikz}
\begin{tikzpicture}
  \begin{axis}[%
    name={fig1},
    scale only axis,
    width=5cm,
    height=2cm,
  ]

    \addplot+[samples=10] {rnd};

  \end{axis}

  \begin{axis}[%
    name={fig2},
    at={($(fig1.south) + (0,-1.5cm)$)},
    anchor=north,
    scale only axis,
    width=5cm,
    height=2cm,
  ]

    \addplot+[samples=10] {rnd};
  \end{axis}

  \begin{axis}[%
    name={fig3},
    at={($(fig2.south) + (0,-1.5cm)$)},
    anchor=north,
    scale only axis,
    width=5cm,
    height=2cm,
  ]
    \addplot+[samples=10] {rnd};
  \end{axis}

  % Martin's answer: https://tex.stackexchange.com/a/14340/32206
  \node (label1) at ($(fig1.north west)$) [above=0.3cm] {\hypertarget{subfig:a}{\subcaptiontext{a}}};
  \node (label2) at ($(fig2.north west)$) [above=0.3cm] {\hypertarget{subfig:b}{\subcaptiontext{b}}};
  \node (label3) at ($(fig3.north west)$) [above=0.3cm] {\hypertarget{subfig:c}{\subcaptiontext{c}}};
\end{tikzpicture}%
\end{filecontents*}

\usepackage{caption}
\usepackage{subcaption}

\usepackage{pgfplots}
\usetikzlibrary{calc,external}
\pgfplotsset{compat=1.10}
% \tikzexternalize % Comment out to get working hyperlinks.

\usepackage[colorlinks]{hyperref}

\begin{document}
Overall figure in Fig.~\ref{fig:figure}. Also see Fig.~\hyperlink{subfig:a}{1a}, Fig.~\hyperlink{subfig:b}{1b} and Fig.~\hyperlink{subfig:c}{1c}.

\begin{figure}
  \centering
  \renewcommand{\figFilename}{figure}
  \tikzsetnextfilename{\figFilename}
  \input{\figFilename.tikz}
  \caption{\protect\hyperlink{subfig:a}{\subcaptiontext{a}} shows this; \protect\hyperlink{subfig:b}{\subcaptiontext{b}} shows that; and \protect\hyperlink{subfig:c}{\subcaptiontext{c}} this and that.}
  \label{fig:\figFilename}
\end{figure}

\end{document}

答案1

超链接不会直接指向子图,但除此之外,它还可以工作。请注意,\phantomsubcaptions 必须放在组 ( {}) 内。

\documentclass{article}

\newcommand{\figFilename}{} % Store the figure filename for convenience.
\newcommand{\subcaptiontext}[1]{\textsf{\textbf{(#1)}}} % Custom label format.

\usepackage{filecontents}
\begin{filecontents*}{figure.tikz}
\begin{tikzpicture}
  \begin{axis}[%
    name={fig1},
    scale only axis,
    width=5cm,
    height=2cm,
  ]

    \addplot+[samples=10] {rnd};

  \end{axis}

  \begin{axis}[%
    name={fig2},
    at={($(fig1.south) + (0,-1.5cm)$)},
    anchor=north,
    scale only axis,
    width=5cm,
    height=2cm,
  ]

    \addplot+[samples=10] {rnd};
  \end{axis}

  \begin{axis}[%
    name={fig3},
    at={($(fig2.south) + (0,-1.5cm)$)},
    anchor=north,
    scale only axis,
    width=5cm,
    height=2cm,
  ]
    \addplot+[samples=10] {rnd};
  \end{axis}

  \node (label1) at ($(fig1.north west)$) [above=0.3cm] {\subcaptiontext{a}};
  \node (label2) at ($(fig2.north west)$) [above=0.3cm] {\subcaptiontext{b}};
  \node (label3) at ($(fig3.north west)$) [above=0.3cm] {\subcaptiontext{c}};

\end{tikzpicture}%
\end{filecontents*}

\usepackage{caption}
\usepackage{subcaption}

\usepackage{pgfplots}
\usetikzlibrary{calc,external}
\pgfplotsset{compat=1.10}
\tikzexternalize % Comment out to get working hyperlinks.

\usepackage[colorlinks]{hyperref}

\begin{document}
Overall figure in Fig.~\ref{fig:figure}. Also see Fig.~\ref{a}, Fig.~\ref{b} and Fig.~\ref{c}.

\begin{figure}
  \centering
  \renewcommand{\figFilename}{figure}
  \tikzsetnextfilename{\figFilename}
  \input{\figFilename.tikz}
{%
  \phantomsubcaption\label{a}
  \phantomsubcaption\label{b}
  \phantomsubcaption\label{c}%
}
  \caption{\subref{a} shows this; \subref{b} shows that; and \subref{c} this and that.}
  \label{fig:\figFilename}
\end{figure}

\end{document}

在此处输入图片描述

附录

如果您为包含子标签的节点定义text width,则似乎\subcaption{}在节点中直接使用也可以。这可以自动增加子标题编号,并与其他子标题保持一致的格式。

\documentclass{article}

\newcommand{\figFilename}{} % Store the figure filename for convenience.
\usepackage{filecontents}
\begin{filecontents*}{figure.tikz}
\begin{tikzpicture}
  \begin{axis}[%
    name={fig1},
    scale only axis,
    width=5cm,
    height=2cm,
  ]

    \addplot+[samples=10] {rnd};

  \end{axis}

  \begin{axis}[%
    name={fig2},
    at={($(fig1.south) + (0,-1.5cm)$)},
    anchor=north,
    scale only axis,
    width=5cm,
    height=2cm,
  ]

    \addplot+[samples=10] {rnd};
  \end{axis}

  \begin{axis}[%
    name={fig3},
    at={($(fig2.south) + (0,-1.5cm)$)},
    anchor=north,
    scale only axis,
    width=5cm,
    height=2cm,
  ]
    \addplot+[samples=10] {rnd};
  \end{axis}

  \node (label1) at ($(fig1.north west)$) [above=0.3cm,text width=2em,align=center] {\subcaption{}};
  \node (label2) at ($(fig2.north west)$) [above=0.3cm,text width=2em,align=center] {\subcaption{}};
  \node (label3) at ($(fig3.north west)$) [above=0.3cm,text width=2em,align=center] {\subcaption{}};

\end{tikzpicture}%
\end{filecontents*}

\usepackage{caption}
\usepackage{subcaption}

\usepackage{pgfplots}
\usetikzlibrary{calc,external}
\pgfplotsset{compat=1.10}
\tikzexternalize % Comment out to get working hyperlinks.

\usepackage[colorlinks]{hyperref}

\begin{document}
Overall figure in Fig.~\ref{fig:figure}. Also see Fig.~\ref{a}, Fig.~\ref{b} and Fig.~\ref{c}.

\begin{figure}
  \centering
  \renewcommand{\figFilename}{figure}
  \tikzsetnextfilename{\figFilename}
  \input{\figFilename.tikz}
{%
  \phantomsubcaption\label{a}
  \phantomsubcaption\label{b}
  \phantomsubcaption\label{c}%
}
  \caption{\subref{a} shows this; \subref{b} shows that; and \subref{c} this and that.}
  \label{fig:\figFilename}
\end{figure}

\end{document}

相关内容