我有以下代码:
\documentclass[12pt,a4paper]{article}
\usepackage[utf8]{inputenc}
\usepackage[T1]{fontenc}
\usepackage{lmodern}
\usepackage{graphicx}
\usepackage{caption}
\usepackage{floatrow}
%\usepackage{caption}
\usepackage{subcaption}
%\usepackage{subfig}
\usepackage{float}
\begin{document}
\noindent
Cross-referencing test: Figure \ref{figure}
\noindent
The figure is \ref{figure_1} and not 1 (a). \\
The figure is \ref{figure_2} and not 1 (b).
\begin{figure}[!ht]
\centering
\begin{subfigure}{0.5\textwidth}
\includegraphics[width=\textwidth]{img/tex.png}
\caption{Caption 1}
\label{figure_1}
\end{subfigure}%
%\qquad
%add desired spacing between images, e. g. ~, \quad, \qquad etc.
%(or a blank line to force the subfigure onto a new line)
\begin{subfigure}{0.5\textwidth}
\centering
\includegraphics[width=\textwidth]{img/tex.png}
\caption{Caption 2}
\label{figure_2}
\end{subfigure}
\caption{Caption of the figure}\label{figure}
\end{figure}
\end{document}
目前,如果我交叉引用文本中的子图,它们会显示如下:1.a
和1.b
。我如何使交叉引用显示如下:1 (a)
和1 (b)
?
答案1
您可以通过在序言中提供以下代码块来实现您的目标。请注意,我假设您希望在图形编号和子图形字母之间留出一个不可分割的空格。
\usepackage{subcaption}
\renewcommand\thesubfigure{(\alph{subfigure})}
\captionsetup[subfigure]{labelformat=simple}
\makeatletter
\renewcommand{\p@subfigure}{\thefigure~} % unbreakable space after fig. num.
\makeatother
完整的 MWE (最小工作示例):
\documentclass[12pt,a4paper]{article}
\usepackage[utf8]{inputenc}
\usepackage[T1]{fontenc}
\usepackage{lmodern}
\usepackage{amssymb} % for "\checkmark" macro
\usepackage[demo]{graphicx} % remove "demo" option in real document
\usepackage{float}
\usepackage{subcaption}
\renewcommand\thesubfigure{(\alph{subfigure})}
\captionsetup[subfigure]{labelformat=simple}
\makeatletter
\renewcommand{\p@subfigure}{\thefigure~} % unbreakable space after fig. #
\makeatother
\begin{document}
\noindent
Basic cross-referencing test: Figure \ref{figure}. \\
1st subfigure cross-ref is \ref{figure_1}. Desired: 1 (a). $\checkmark$ \\
2nd subfigure cross-ref is \ref{figure_2}. Desired: 1 (b). $\checkmark$
\begin{figure}[!ht]
\begin{subfigure}{0.475\textwidth}
\includegraphics[width=\textwidth]{img/tex.png}
\caption{Caption 1}
\label{figure_1}
\end{subfigure}%
\hfill % maximize the horizontal separation
\begin{subfigure}{0.475\textwidth}
\includegraphics[width=\textwidth]{img/tex.png}
\caption{Caption 2}
\label{figure_2}
\end{subfigure}
\caption{Overall Caption}
\label{figure}
\end{figure}
\end{document}