如何找出我的浮動內容所在的頁面(無需查看文件的每一頁)?

如何找出我的浮動內容所在的頁面(無需查看文件的每一頁)?

這個問題導致了一個新的包:
chkfloat

對我的(兩列)文件進行小的調整可能會導致我的浮動(單列和雙列)到處移動。我知道如果 floatF在頁面上P,我的文件比在 page 上看起來更美觀P+1。然而,我有一個很長的文檔,每次構建後檢查每一頁以查看我的浮動是否已移動是很煩人的。

F(n)因此,如果浮動不在頁面上,我希望 LaTeX 提出錯誤(或至少以某種方式提醒我),其中P(n)F(n)P(n)為我的浮動的某些子集指定的(我很高興能得到一個可以處理跟踪的答案)不過一開始就只有一個漂浮物!但是,不可能將檢查程式碼放入浮點數本身中,因為程式碼是在「讀取」浮點數時執行的,而不是在「放置」浮點數時執行的。

即使我可以調整浮動參數,以便浮動出現在我喜歡的位置,這仍然是一個有用的自動化測試,所以我的問題的焦點並不是關於浮動參數,除非有一些神奇的參數強制LaTeX 將浮標放置在我指定的大致位置(即,浮動位置!——不僅僅是“這裡”,我認為這對於兩列浮標來說是不可能的)。

顯然,LaTeX 確實會追蹤哪些浮動出現在哪個頁面上,因為\listoffigures可以顯示此資訊。但我不確定自己訪問該資訊以進行測試的最佳方式是什麼;當放置浮動時是否有一個鉤子被呼叫?最好該測試可以在一次運行中進行(請注意,圖形列表需要兩次運行才能更新),但這不是必需的。

我已經包含了一個單柱 MWE,其中有一個浮標可供使用。變更\lipsum範圍會改變浮動的放置位置。 (如果雙列性質產生很大差異,我可以嘗試提供修改後的 MWE。)

\documentclass{article}
\usepackage[a4paper]{geometry}%don't trust MikTeX's defaults!
\usepackage{lipsum}
\begin{document}
    % choose which of the following two lines to use to configure where the float appears.
    \lipsum[1-5]% figure appears on page 2 if this line is used
    %\lipsum[1-4]% figure appears on page 1 if this line is used
    \begin{figure}
        \centering
        Hello world. I was typeset on page \thepage, but that may not be where I appear!

        \rule{4cm}{4cm}

        \caption{fig}
    \end{figure}
    ***Figure appeared in the text just above here.***

    \lipsum[5-8]
    \listoffigures
\end{document}

答案1

與 JLDIaz 不同的方法。只需將程式碼放在序言之間START HEREEND HERE末尾即可。程式碼是自我註解的。

評論:我把這個做成了一個包包。不要直接使用此程式碼,更好獲取並使用該包

\documentclass{article}

% START HERE
\makeatletter
% tolerances
\PassOptionsToPackage{patch}{kvoptions}
\RequirePackage{kvoptions}
\DeclareStringOption{tolerance}
\def\chkfloat@tolerance{1}
\ProcessKeyvalOptions*
% store original macros \@float and \@caption
\let\chkfloat@float\@float
\let\chkfloat@caption\@caption
% make \@float remember the page where the float should be
\def\@float{\edef\chkfloat@page{\thepage}\chkfloat@float}
% make \@caption write to a file .fof the information about final page, original page and float caption
\def\@caption#1[#2]#3{\chkfloat@caption{#1}[#2]{#3}%
  \addtocontents{fof}{\protect\chkfloat@{\thepage}{\chkfloat@page}{\csname fnum@#1\endcsname: #2}}%
}
% checking macro
\def\chkfloat@#1#2#3{\ifnum#1>\numexpr#2+\chkfloat@tolerance\relax
  \begingroup\let\on@line\@gobble\def\nobreakspace{ }\GenericWarning{}{Float misplaced on pages #2->#1, #3}\endgroup
\fi
}
% process the file .fof
\@starttoc{fof}

\makeatother
% END HERE

\begin{document}

Hello world!

\begin{figure}[b]
FIGURE BOTTOM
\caption{Figure at bottom}
\end{figure}

\begin{figure}[p]
FIGURE PAGE
\caption{Figure at its page}
\end{figure}

\end{document}

答案2

這是一個想法。當您使用\label標題旁的指令時,LaTeX 會將.aux圖形編號和頁碼(圖形出現的實際頁碼)寫入檔案中。此外,它定義了一個儲存該頁碼的宏,可以使用 command 檢索該頁碼\pageref

這個想法是為每個圖形添加一個\label指定該圖形應出現的頁面,例如:\label{ShouldBeOnPage1}。外部腳本可以讀取該-aux檔案並將標籤名稱與其頁碼值進行比較。

更好的是,LaTeX 可以在文件末尾進行此檢查。下面的 MWE 提供了基本思想,但它只檢查單一圖形。正確的解決方案將包括一個循環來檢查所有數字,但我缺乏實現完整解決方案的知識。您可以像我對第一張圖所做的那樣手動指定每項檢查:

\documentclass{article}
\usepackage[a4paper]{geometry}%don't trust MikTeX's defaults!
\usepackage{lipsum}

\def\CheckFloat#1{%  The macro which does the check for a single figure
% The parameter is the page number at which the figure should appear
\expandafter\ifx#1\pageref{ShouldBeOnPage#1}%
\relax
\else\GenericWarning{(Floats)}{Warning: A float which should appear on page #1 is misplaced (to page \pageref{ShouldBeOnPage#1})}
\fi
}


\AtEndDocument{% Manually do all checks
 \CheckFloat{1}
}

\begin{document}
% choose which of the following two lines to use to configure where the float appears.
\lipsum[1-5]% figure appears on page 2 if this line is used
%\lipsum[1-4]% figure appears on page 1 if this line is used
\begin{figure}
\centering
Hello world. I was typeset on page \thepage, but that may not be where I appear!

\rule{4cm}{4cm}

\caption{fig}\label{ShouldBeOnPage1}  % <----- Add this to the figures
\end{figure}
***Figure appeared in the text just above here.***

\lipsum[5-8]
\listoffigures
\end{document}

運行此範例您會收到警告:

Warning: A float which should appear on page 1 is misplaced (to page 2\hbox {})
 on input line 33.

請注意,我的程式碼假設每頁只有一個數字。正確的解決方案是根據圖形編號為每個圖形定義一個宏,該宏儲存圖形應出現的頁面,並根據圖形編號和預期頁面建立一個標籤,該標籤將透過以下方式提供實際頁面\pageref

我知道這不是一個完整的解決方案,但我希望這個想法是有價值的,並且有比我更熟練的人來完成它。

相關內容