是否可以完全独立地格式化子\ref
标题\subref
?
手册建议更改thesubfigure
子图的标签,从而更改的输出\ref
,但这也会产生影响\subref
。
例如,以下 MWE
\documentclass{article}
\usepackage{subcaption}
\usepackage{cleveref}
\usepackage{tikz}
% and example figure
\def\exfigure#1#2{
\begin{subfigure}[b]{0.3\linewidth}
\centering
\begin{tikzpicture}\path[draw=#1,fill=#1!20] (0,0) rectangle
(4,4);\end{tikzpicture}
\renewcommand{\thesubfigure}{#1}%
\phantomsubcaption%
\label{#2}%
\end{subfigure}\hfill}
\DeclareCaptionLabelFormat{subreffig}{(\emph{#2})}
\captionsetup{subrefformat=subreffig}
\begin{document}
\begin{figure}
\exfigure{red}{subfig1}
\exfigure{green}{subfig2}
\exfigure{blue}{subfig3}
\caption{Three squares. Left: \subref{subfig1}. Center: \subref{subfig2}.
Right: \subref{subfig3}.}
\label{fig:all}
\end{figure}
In \cref{fig:all}, we have three subfigures \cref{subfig1}
\cref{subfig2} and \cref{subfig3}.
\end{document}
该图有以下标题
图 1:三个正方形。左图:(红色的)。 中心: (绿色的)。 正确的: (蓝色的)。
这是我想要的输出。但是,后面的文本有
在图 1 中,我们有三个子图:图 1red、图 1green 和图 1blue。
是否可以将其更改为
在图 1 中,我们有三个子图:图 1-[红色]、图 1-[绿色] 和图 1-[蓝色]。
\thesubfigure
将的定义更改为
\renewcommand{\thesubfigure}{-[#1]}%
在文本中给出正确的输出,但在标题中给出错误的输出。
答案1
我可以让它在没有 cleveref 的情况下工作。subcaption 和 cleveref(更不用说 hyperref)都\label
以多种方式进行修改,在 aux 文件中产生大量\newlabel
命令。所以我回到基础并改用\protected@write
。
\documentclass{article}
\usepackage{subcaption}
%\usepackage{cleveref}
\usepackage{tikz}
\makeatletter
\newcommand{\sublabel}[1]{\refstepcounter{subfigure}%
\protected@write\@auxout{}{\string\newlabel{sub@#1}{{(\string\emph{\thesubfigure})}{\thepage}}}%
\protected@write\@auxout{}{\string\newlabel{#1}{{\thefigure-[\thesubfigure]}{\thepage}}}}
\makeatother
% and example figure
\def\exfigure#1#2{
\begin{subfigure}[b]{0.3\linewidth}
\centering
\begin{tikzpicture}\path[draw=#1,fill=#1!20] (0,0) rectangle
(4,4);\end{tikzpicture}
\renewcommand{\thesubfigure}{#1}%
\sublabel{#2}%
\end{subfigure}\hfill}
\begin{document}
\begin{figure}
\exfigure{red}{subfig1}
\exfigure{green}{subfig2}
\exfigure{blue}{subfig3}
\caption{Three squares. Left: \subref{subfig1}. Center: \subref{subfig2}.
Right: \subref{subfig3}.}
\label{fig:all}
\end{figure}
In \ref{fig:all}, we have three subfigures \ref{subfig1}
\ref{subfig2} and \ref{subfig3}.
\end{document}
此版本兼容 cleveref 和 hyperref。关键是格式化\thesubfigure
和\cref
和。\@currentlabel
\ref
\subref
\documentclass{article}
\usepackage{tikz}
\usepackage{subcaption}
\usepackage{hyperref}
\usepackage{cleveref}
\makeatletter
\newcommand{\sublabel}[2]{% #1 = color, #2 = label
\refstepcounter{subfigure}%
\edef\@currentlabel{(\noexpand\emph{#1})}\nosublabel{sub@#2}%
\edef\@currentlabel{\thefigure-[#1]}\nosublabel{#2}%
\ignorespaces}
\makeatother
% and example figure
\def\exfigure#1#2{\let\nosublabel=\label
\begin{subfigure}[b]{0.3\linewidth}
\centering
\begin{tikzpicture}\path[draw=#1,fill=#1!20] (0,0) rectangle
(4,4);\end{tikzpicture}
\renewcommand{\thesubfigure}{-[#1]}% for cref
\sublabel{#1}{#2}
\end{subfigure}\hfill}
\begin{document}
\begin{figure}
\exfigure{red}{subfig1}
\exfigure{green}{subfig2}
\exfigure{blue}{subfig3}
\caption{Three squares. Left: \subref{subfig1}. Center: \subref{subfig2}.
Right: \subref{subfig3}.}
\label{fig:all}
\end{figure}
In \cref{fig:all}, we have three subfigures \cref{subfig1}
\cref{subfig2} and \cref{subfig3}.
\end{document}
\begin{document}
\begin{figure}
\exfigure{red}{subfig1}
\exfigure{green}{subfig2}
\exfigure{blue}{subfig3}
\caption{Three squares. Left: \subref{subfig1}. Center: \subref{subfig2}.
Right: \subref{subfig3}.}
\label{fig:all}
\end{figure}
In \cref{fig:all}, we have three subfigures \cref{subfig1}
\cref{subfig2} and \cref{subfig3}.
\end{document}
答案2
基于 subcaption 内部的可能解决方案
\documentclass{article}
\usepackage{subcaption}
\usepackage{cleveref}
\usepackage{tikz}
\newcommand{\thesubreffigure}{\thesubfigure}
\makeatletter
\renewcommand*\subcaption@@label[2]{%
\@bsphack\begingroup
\subcaption@ORI@label#1{#2}%
\let\SK@\@gobbletwo
\protected@edef\@currentlabel{\csname thesubref\@captype\endcsname}%
\subcaption@ORI@label#1{sub@#2}%
\endgroup\@esphack}
\makeatother
% and example figure
\def\exfigure#1#2{
\begin{subfigure}[b]{0.3\linewidth}
\centering
\begin{tikzpicture}\path[draw=#1,fill=#1!20] (0,0) rectangle
(4,4);\end{tikzpicture}
% \renewcommand{\thesubfigure}{#1}
%
\renewcommand{\thesubfigure}{-[#1]}%
\renewcommand{\thesubreffigure}{#1}%
\phantomsubcaption%
\label{#2}%
\end{subfigure}\hfill}
\DeclareCaptionLabelFormat{subreffig}{(\emph{#2})}
\captionsetup{subrefformat=subreffig}
\begin{document}
\begin{figure}
\exfigure{red}{subfig1}
\exfigure{green}{subfig2}
\exfigure{blue}{subfig3}
\caption{Three squares. Left: \subref{subfig1}. Center: \subref{subfig2}.
Right: \subref{subfig3}.}
\label{fig:all}
\end{figure}
In \cref{fig:all}, we have three subfigures \cref{subfig1}
\cref{subfig2} and \cref{subfig3}.
\end{document}
\cref
为和生成正确的输出\subref
。遗憾的是,当设置标题而不是使用 时,不会生成正确的标签\phantomsubcaption
。