因为我希望标题的宽度与表格和图片的宽度相同,所以我使用了包threeparttable
和measuredfigure
环境。但是,由于我在文本中引用和标记表格和图片时遇到了问题。基本上,图片/表格编号被替换为 ??。我在网上搜索过,但找不到解决此问题所需的适当帮助。
如果我从环境中删除标题和标签measuredfigure
并将其放在正上方,\centering
它可以正常工作,但标题不再是表格/图形的宽度。
非常感谢
埃格姆1b
这是 WME
\documentclass[12pt,a4paper,oneside,english,oldfontcommands]{memoir}
\usepackage[utf8]{inputenc}
\usepackage[T1]{fontenc}
\usepackage{lmodern}
\usepackage[english]{babel}
\usepackage{xcolor}
\usepackage{csquotes}
\usepackage{amsmath}
\usepackage{amsfonts}
\usepackage{amssymb}
\usepackage{graphicx}
\usepackage{booktabs}
\usepackage{siunitx}
\usepackage{gensymb}
\usepackage{tikz}
\usepackage{wasysym}
\usepackage{tabularx}
\usepackage{cleveref}
\usepackage{threeparttable}
\usepackage{caption}
\captionsetup{justification = raggedright,
singlelinecheck = false}
\newsubfloat{figure}% Allow subfloats in figure environment
\begin{document}
Below is summary table \Cref{tab:marker}
% Table generated by Excel2LaTeX from sheet 'Sheet6'
\begin{table}
\centering
\begin{measuredfigure}
\caption{Caribou age groups at Nunalleq based on epiphyseal fusion \label{tab:marker}}
\begin{tabular}{lcc}
\toprule
Age group & NISP & \% NISP \\
\midrule
Adult (fused) & 441 & 88.6\% \\
Subadult (unfused) & 46 & 9.2\% \\
Juvenile (unfused) & 11 & 2.2\% \\
\midrule
Total & 498 & 100.0\% \\
\bottomrule
\end{tabular}%
\end{measuredfigure}
\end{table}%
以及图形的 MWE 代码
Here is example with reference to \Cref{marker2}
\begin{figure}
\centering
\begin{measuredfigure}
\includegraphics[width=5cm]{figures/ch7_caribouwear1}
\caption{This is a very long caption very long}
\label{marker2}
\end{measuredfigure}
\end{figure}
\end{document}
答案1
如果其他人仍然遇到此问题,我已经找到了一种解决方法,即使用幻影字幕和重置图形计数器。它可能不太优雅,但似乎有效,至少对我来说是这样。
\documentclass[12pt,a4paper,oneside,english]{article}
\usepackage[T1]{fontenc}
\usepackage[english]{babel}
\usepackage{cleveref}
\usepackage{threeparttable}
\usepackage{caption}% necessary for \phantomcaption
\usepackage[allfiguresdraft]{draftfigure}% dummy figures
\begin{document}
Below is an example with a reference to \Cref{fig:ex1}:
\begin{figure}[h]
\centering
\begin{measuredfigure}
\includegraphics[draft=false,width=5cm]{example-image-a}
\caption{This is a very, very, very long caption.}
\end{measuredfigure}
\addtocounter{figure}{-1}
\phantomcaption
\label{fig:ex1}
\end{figure}
And here is another example with a reference to \Cref{fig:ex2}:
\begin{figure}[h]
\centering
\begin{measuredfigure}
\includegraphics[draft=false,width=5cm]{example-image-b}
\caption{This is another very, very, very long caption.}
\end{measuredfigure}
\addtocounter{figure}{-1}
\phantomcaption
\label{fig:ex2}
\end{figure}
\end{document}
答案2
在我看来,这看起来像是一个错误,即使在简单的文档中,measuredfigure 也会丢失标签。但通常你知道图像的宽度,所以你不需要环境,可以使用 minipage:
\documentclass[]{book}
\usepackage[utf8]{inputenc}
\usepackage[T1]{fontenc}
\usepackage[english]{babel}
\usepackage{threeparttable,graphicx}
\begin{document}
Here is example with reference to \ref{fig:marker1} %missing
\begin{figure}
\centering
\begin{measuredfigure}
\includegraphics[width=5cm]{example-image}
\caption{This is a very long caption very long\label{fig:marker1}}
\end{measuredfigure}
\end{figure}
Here is example with reference to \ref{fig:marker2}
\begin{figure}
\centering
\begin{minipage}{5cm}
\includegraphics[width=\linewidth]{example-image}
\caption{This is a very long caption very long\label{fig:marker2}}
\end{minipage}
\end{figure}
\end{document}
答案3
感谢 MC Kosh,您的解决方法非常有效。
顺便提一下,为了使其更加用户友好,我在 tex 文件的开头定义了如下函数:
\newcommand*\MeasuredFigureLabel[1]{
\addtocounter{figure}{-1}
\phantomcaption
\label{#1}
}
这使得这种方法非常可行。这样,即使必须包含许多图形,一切看起来仍然很干净。这是示例图的调整后的包含内容:
\begin{figure}[h]
\centering
\begin{measuredfigure}
\includegraphics[draft=false,width=5cm]{example-image-a}
\caption{This is another very, very, very long caption.}
\end{measuredfigure}
\MeasuredFigureLabel{fig:ex1}
\end{figure}
答案4
我建议直接放弃measuredfigure
并使用caption
包。它的\captionbox
命令可确保标题与图像具有相同的宽度。
\documentclass{book}
\usepackage{graphicx}
\usepackage{caption}
\usepackage{lipsum}
\begin{document}
\begin{figure}[ht]
\centering
\captionbox{\lipsum[23]\label{img}}%
{\includegraphics[width=0.5\textwidth]{example-image}}%
\end{figure}
Image reference: \ref{img}.
\end{document}
毋庸置疑,该caption
软件包的主要目的实际上是提供无限的字幕格式化可能性,因此您可能会发现它也解决了您的许多其他问题。它是一款广受欢迎、维护良好且兼容性良好的软件包。
threeparttable
之后,完全放弃(有些过时的)包并迁移到 是有意义的tabularray
。它的talltblr
表threeparttable
很好地整合了功能,以及聚宝盆其他功能。