使用 endfloat 時,使用 \caption 下面的 \includegraphics 渲染圖形上方的標題

使用 endfloat 時,使用 \caption 下面的 \includegraphics 渲染圖形上方的標題

背景:我的 TeX 檔案是從 R Markdown 產生的,標題自動放置在包含的圖形下方。下面是我產生的 TeX 檔案的一個最小範例:

\documentclass[man]{apa7}
\title{Test}

\begin{document}
\maketitle

Lorem ipsum

\begin{figure}
The figure
\caption{The caption.}
\end{figure}

\end{document}

問題:需要渲染圖形標題多於相應的數字(根據 APA 指南),無需移動\caption

我嘗試過的floatrow:我知道標題可以呈現在圖上方,而無需透過套件和更改程式碼\floatsetup[figure]{style=plaintop}。然而,加載floatrow會幹擾endfloat,而 是由 加載的apa7。具體來說,數字不再放置在文件的末尾,而是呈現在適當的位置:

\documentclass[man]{apa7}
\usepackage{floatrow}
\floatsetup[figure]{style=plaintop}

\title{Test}

\begin{document}
\maketitle

Lorem ipsum

\begin{figure}
The figure
\caption{The caption.}
\end{figure}

\end{document}

根據 的文檔endfloatfloatrow應始終在先前加載endfloat(因此,在 之前apa7)。因此,我嘗試floatrow通過加載\RequirePackage{},但這會產生錯誤。我可以透過取消定義兩個長度來修復其中一些問題,但這給我帶來了以下我似乎無法解決的錯誤:

! Missing \endcsname inserted.
<to be read again> 
                   \@classoptionslist 
l.1453 \ProcessOptionsWithKV{floatrow}

這是最小的可重現範例:

\RequirePackage{floatrow}
\let\abovecaptionskip\undefined
\let\belowcaptionskip\undefined

\documentclass{apa7}

\begin{document}

Lorem ipsum

\end{document}

請注意,儘管出現錯誤訊息,我還是得到了一個看起來符合預期的渲染 PDF 檔案。這也不是特定於apa7;當我使用articleor文檔類別時,我得到同樣的錯誤book

答案1

有趣的是,apa7 轉換figurefigure*.無論如何,基本思想是將標題和圖形儲存到單獨的保存箱中並顛倒它們的順序。

\documentclass[man]{apa7}
%\documentclass{article}
%\usepackage{endfloat}
\usepackage{lipsum}% MWE only

% udbox is a \vbox version of lrbox
\makeatletter
\def\udbox#1{%
  \edef\reserved@a{%
  \endgroup
  \setbox#1\vbox{%
  \begingroup\aftergroup}%
  \def\noexpand\@currenvir{\@currenvir}%
  \def\noexpand\@currenvline{\on@line}}%
  \reserved@a
  \@endpefalse
  \color@setgroup
  \ignorespaces}
\def\endudbox{\unskip\color@endgroup}
\makeatother

\newsavebox{\mycaptionbox}
\newsavebox{\myfigurebox}

\makeatletter
\let\normalmakecaption=\@makecaption
\def\@makecaption#1#2{\def\test{figure}%
  \ifx\@captype\test \global\setbox\mycaptionbox=\vbox{\normalmakecaption{#1}{#2}}%
  \else \normalmakecaption{#1}{#2}%
  \fi}
\makeatother

\let\normalfigure=\figure
\let\endnormalfigure=\endfigure

\renewenvironment{figure}[1][tbp]{\normalfigure
  \begin{udbox}{\myfigurebox}}%
{\end{udbox}\unvbox\mycaptionbox
  \unvbox\myfigurebox\endnormalfigure}

\expandafter\let\expandafter\normalfigurestar\csname figure*\endcsname
\expandafter\let\expandafter\endnormalfigurestar\csname endfigure*\endcsname

\renewenvironment{figure*}[1][tbp]{\normalfigurestar
  \begin{udbox}{\myfigurebox}}%
{\end{udbox}\unvbox\mycaptionbox
  \unvbox\myfigurebox\endnormalfigurestar}

\title{Test}

\begin{document}
\maketitle

Lorem ipsum

\begin{figure}
\lipsum[1]
\caption{The caption.}
\end{figure}

\end{document}

答案2

包正在內部efloat重新定義figure等,通常定義為或。在這種情況下,兩者都會過早地進行重新定義,因為套件進行了重新定義。因此,解決您的情況的技巧是進一步延遲包完成的重新定義。figure*\efloat@AtBeginDocument\efloat@AtBeginDocument\@iden\AtBeginDocumentfloatrow\AtBeginDocumentendfloat

幸運的是, 的定義\efloat@AtBeginDocument已經完成,\providecommand因為 的 作者efloat已經考慮為用戶提供一個選項來自行確定應該完成重新定義的確切時刻:“(注意:\efloat@AtBeginDocument 將使用 \ 定義提供命令,以便文檔類和包可以在需要時預先定義它efloat

下面的解決方案定義了它自己的版本\efloat@AtBeginDocument,將內容儲存到一個名為 的巨集中\efloatredefinitions,該巨集可以稍後應用,特別是在套件的重新定義floatrow已經完成之後。

% Store the code of efloat re-definitions into \efloatredefinitions
% (This code must be defined before the efloat package is loaded.)
\makeatletter
\newcommand\efloatredefinitions{}
\newcommand\efloat@AtBeginDocument{\g@addto@macro\efloatredefinitions}
\makeatother

\documentclass[man]{apa7}

\usepackage{floatrow}
\floatsetup[figure]{style=plaintop}

% Do the efloat re-definitions after the re-definitions of floatrow were done
%\show\efloatredefinitions
\AtBeginDocument{\efloatredefinitions} % or simply \efloatredefinitions after \begin{document}

\title{Test}

\begin{document}
\maketitle

Lorem ipsum

\begin{figure}
The figure
\caption{The caption.}
\end{figure}

\end{document}

endfloatPS:如果套件能夠提供「storeredefinitions」或類似選項,那就太好了,這樣\PassOptionsToPackage{storeredefinitions}{endfloat}就可以使用而不是\efloat@AtBeginDocument自己定義。會寫一封電子郵件給作者endfloat關於這個​​......

相關內容