描述
对于标准figure
环境,如果标题适合一行,则标题居中,否则标题左对齐。
然而,当使用figure*
来自sidenotes
包,这种行为不存在:两种情况下标题都是左对齐的。
问题
figure*
如果标题只占一行,如何才能让它们居中(重要的:如果可能的话,我希望线宽等于文本宽度 + 边距宽度。长度超过文本宽度但小于文本宽度 + 边距宽度的线应算作单行)?到目前为止,我尝试过但似乎不起作用的是
\usepackage[singlelinecheck=true]{caption}
或者
\DeclareCaptionStyle{widefigure}{singlelinecheck=true}
任何帮助都将受到赞赏。
梅威瑟:
\documentclass[paper=a4]{scrartcl}
\usepackage[utf8]{inputenc}
% to fit both figures on one page
\usepackage[left=6cm, right=6cm, top=1cm, bottom=1cm]{geometry}
\usepackage{showframe}
\usepackage{sidenotes}
\usepackage{graphicx}
% neither option works
%\usepackage[singlelinecheck=true]{caption}
%\DeclareCaptionStyle{widefigure}{singlelinecheck=true}
\begin{document}
\begin{figure*}[h]
\centering
\includegraphics[width=\linewidth]{example-image-a}
\caption{A figure}
\label{fig:m}
\end{figure*}
\begin{figure*}[h]
\centering
\includegraphics[width=\linewidth]{example-image-a}
\caption{A figure with very very very very long text it does not even stop? Will it ever stop? It should be long enough to not fit into a single line, at least I hope so.}
\label{fig:m}
\end{figure*}
\begin{figure}[h]
\centering
\includegraphics[width=\linewidth]{example-image-a}
\caption{A figure}
\label{fig:m}
\end{figure}
\begin{figure}[h]
\centering
\includegraphics[width=\linewidth]{example-image-a}
\caption{A figure with very very very very long text it does not even stop? Will it ever stop? It should be long enough to not fit into a single line, at least I hope so.}
\label{fig:m}
\end{figure}
\end{document}
答案1
\DeclareCaptionStyle
如果检测到单行,则有一个可选参数用于设置。您应该在那里设置居中。
caption 用于\hsize
测量和居中,但 adjustwidth 不设置它。我在这里使用钩子将其设置\hsize
为\linewidth
图形内部*,这需要新的 LaTeX。
\documentclass[paper=a4]{scrartcl}
\usepackage[utf8]{inputenc}
% to fit both figures on one page
\usepackage[left=6cm, right=6cm, top=1cm, bottom=1cm]{geometry}
\usepackage{showframe}
\usepackage{sidenotes}
\usepackage{graphicx}
\DeclareCaptionStyle{widefigure}[justification=centering]{font=footnotesize}
\AddToHook{env/figure*/begin}{\AddToHookNext{cmd/captionsetup/before}{\hsize\linewidth}}
\begin{document}
\begin{figure*}[h]
\centering
\includegraphics[width=\linewidth]{example-image-a}
\caption{A figure}
\label{fig:m}
\end{figure*}
\begin{figure*}[h]
\centering
\includegraphics[width=\linewidth]{example-image-a}
\caption{A figure with very very very very long text it does not even stop? Will it ever stop? It should be long enough to not fit into a single line, at least I hope so.}
\label{fig:m}
\end{figure*}
\begin{figure}[h]
\centering
\includegraphics[width=\linewidth]{example-image-a}
\caption{A figure}
\label{fig:m}
\end{figure}
\begin{figure}[h]
\centering
\includegraphics[width=\linewidth]{example-image-a}
\caption{A figure with very very very very long text it does not even stop? Will it ever stop? It should be long enough to not fit into a single line, at least I hope so.}
\label{fig:m}
\end{figure}
\end{document}
答案2
我最终放弃修复侧注并简单地创建了我自己的图形*环境。
\documentclass[paper=a4]{scrartcl}
\usepackage[utf8]{inputenc}
% to fit both figures on one page
\usepackage[left=6cm, right=6cm, top=1cm, bottom=1cm]{geometry}
\usepackage{showframe}
\usepackage{ifoddpage}
\usepackage{graphicx}
\newsavebox{\widefigurebox}
\renewenvironment{figure*}[1][tbp]{\figure[#1]%
\begin{lrbox}{\widefigurebox}%
\minipage{\dimexpr \textwidth+\marginparsep+\marginparwidth\relax}}%
{\endminipage\end{lrbox}\checkoddpage
\ifoddpage
\usebox{\widefigurebox}\hspace{\dimexpr -\marginparsep-\marginparwidth}%
\else
\hspace{\dimexpr -\marginparsep-\marginparwidth}\usebox{\widefigurebox}%
\fi
\endfigure}
\begin{document}
\begin{figure*}[h]
\centering
\includegraphics[width=\linewidth]{example-image-a}
\caption{normal caption}
\label{fig:m}
\end{figure*}
\begin{figure*}[h]
\centering
\includegraphics[width=\linewidth]{example-image-a}
\caption{A figure with very very very very long text it does not even stop? Will it ever stop? It should be long enough to not fit into a single line, at least I hope so.}
\label{fig:m}
\end{figure*}
\begin{figure}[h]
\centering
\includegraphics[width=\linewidth]{example-image-a}
\caption{A figure}
\label{fig:m}
\end{figure}
\begin{figure}[h]
\centering
\includegraphics[width=\linewidth]{example-image-a}
\caption{A figure with very very very very long text it does not even stop? Will it ever stop? It should be long enough to not fit into a single line, at least I hope so.}
\label{fig:m}
\end{figure}
\end{document}
此版本将标题保留在文本区域内,而不是扩展到 marginpar 区域。我还决定使用 widefigure 而不是 figure*。
\documentclass[paper=a4]{scrartcl}
\usepackage[utf8]{inputenc}
% to fit both figures on one page
\usepackage[left=6cm, right=6cm, top=1cm, bottom=1cm]{geometry}
\usepackage{showframe}
\usepackage{ifoddpage}
\usepackage{graphicx}
\newsavebox{\widefigurebox}
\newenvironment{widefigure}[1][tbp]{\figure[#1]%
\let\oldcaption=\caption
\edef\oldwidth{\the\textwidth}%
\checkoddpage
\ifoddpage
\renewcommand{\caption}[2][\empty]{\parbox{\oldwidth}{\ifx\empty##1\relax\oldcaption{##2}%
\else\oldcaption[##1]{##2}\fi}\hfill}%
\else
\renewcommand{\caption}[2][\empty]{\hfill\parbox{\oldwidth}{\ifx\empty##1\relax\oldcaption{##2}%
\else\oldcaption[##1]{##2}\fi}}%
\fi
\begin{lrbox}{\widefigurebox}%
\minipage{\dimexpr \textwidth+\marginparsep+\marginparwidth}}% body here
{\endminipage\end{lrbox}%
\ifoddpage
\usebox\widefigurebox\hspace{\dimexpr -\marginparsep-\marginparwidth}%
\else
\hspace{\dimexpr -\marginparsep-\marginparwidth}\usebox\widefigurebox
\fi
\endfigure}
\begin{document}
\begin{widefigure}[h]
\centering
\includegraphics[width=\linewidth]{example-image-a}
\caption{normal caption}
\label{fig:m}
\end{widefigure}
\begin{widefigure}[h]
\centering
\includegraphics[width=\linewidth]{example-image-a}
\caption{A figure with very very very very long text it does not even stop? Will it ever stop? It should be long enough to not fit into a single line, at least I hope so.}
\label{fig:m}
\end{widefigure}
\begin{figure}[h]
\centering
\includegraphics[width=\linewidth]{example-image-a}
\caption{A figure}
\label{fig:m}
\end{figure}
\begin{figure}[h]
\centering
\includegraphics[width=\linewidth]{example-image-a}
\caption{A figure with very very very very long text it does not even stop? Will it ever stop? It should be long enough to not fit into a single line, at least I hope so.}
\label{fig:m}
\end{figure}
\end{document}