在此主题中:
单个图形的子图形标签
我建议了一个解决方案,如何将自定义字符串包含到命令创建的超链接中\cref
。
我想创建一个紧凑的新命令,它将采用标签和自定义字符串。例如:\crefSugFigRef{\fig:label}{customstring}
然而,
\newcommand{\crefSugFigRef}[2]{\crefformat{figure}{fig.~#2#1{(#2)}#3}\cref{#1}\crefformat{figure}{fig.~#2#1#3}}
无法工作,因为和都\newcommand
使用\crefformat
字符串#1
和#2
。
有人知道如何让它工作吗?
答案1
使用你在问题中指出的概念单个图形的子图形标签,我认为这就是您想要实现的自动化\cref
变更。
\documentclass{article}
\usepackage{hyperref}
\usepackage{cleveref}
\usepackage{mwe}
\usepackage{graphicx}
\newcommand{\crefSubFigRef}[2]{\crefformat{figure}{fig.~##2##1{(#2)}##3}%
\cref{#1}\crefformat{figure}{fig.~##2##1##3}}
\begin{document}
\begin{figure}
\centering
\includegraphics[width=\textwidth]{example-image-a}
\caption{Main caption here}
\label{fig:main}
\end{figure}
CREF CHANGED BY HAND:
The default format of \texttt{cref} looks like this \cref{fig:main}.
The format of the figure reference is now changed on the fly to be%
\crefformat{figure}{fig.~#2#1{(a)}#3} % change the format to include (a)
\cref{fig:main} or%
\crefformat{figure}{fig.~#2#1{(b)}#3} % change the format to include (b)
\cref{fig:main}
\crefformat{figure}{fig.~#2#1#3}% restore the default format
instead of \cref{fig:main}.
AUTO-CHANGED:
The default format of \texttt{cref} looks like this \cref{fig:main}.
The format of the figure reference is now changed on the fly to be
\crefSubFigRef{fig:main}{a}
or
\crefSubFigRef{fig:main}{b}
instead of \cref{fig:main}.
\end{document}
我发现这种方法的一个好处是,人们可以使用任何不符合 hyperref 的方法来构建子图,并且仍然通过这种方式超链接到图形,如下所示:
\documentclass{article}
\usepackage{hyperref}
\usepackage{cleveref}
\usepackage{mwe}
\usepackage{graphicx}
\usepackage{stackengine}
\newcommand{\crefSubFigRef}[2]{\crefformat{figure}{fig.~##2##1{(#2)}##3}%
\cref{#1}\crefformat{figure}{fig.~##2##1##3}}
\begin{document}
\begin{figure}[ht]
\centering
\stackunder{\includegraphics[width=2in]{example-image-a}}{(a)}~~
\stackunder{\includegraphics[width=2in]{example-image-a}}{(b)}
\caption{Main caption here for (a) first subfigure and (b) second
subfigure\label{fig:main}}
\end{figure}
\clearpage
The default format of \texttt{cref} looks like this \cref{fig:main}.
The format of the figure reference is now changed on the fly to be
\crefSubFigRef{fig:main}{a}
or
\crefSubFigRef{fig:main}{b}
instead of \cref{fig:main}.
\end{document}