如何使用页码代替 \thefigure 来表示图表

如何使用页码代替 \thefigure 来表示图表

我想做个小实验。我想用文档中的页码来对图片进行编号。当文本中提到“图 34.2”时,这意味着查看第 34 页的第二张图。您能建议我如何实现这一点吗?

现在,我知道页码是在输出阶段完成的,所以我认为我无法直接重新定义与图形相关的宏。我甚至怀疑这可能必须分两次完成。

非常感谢。

答案1

\documentclass{article}
\usepackage[utf8]{inputenc}
\usepackage{graphicx}

% \usepackage{bophook} % Bophook is apparently old? But it works I guess
%     \AtBeginPage{%
%         \setcounter{figure}{0}
%     }

\AddToHook {shipout/background}{\setcounter{figure}{0}} %% Happens at the start of every page

\usepackage{subcaption}
    \DeclareCaptionFormat{page}
    {%
        \textbf{Figure \thepage.\thefigure #2}#3
    }
    \captionsetup{format=page}

\begin{document}
\begin{figure}[h!]
\centering
  \includegraphics[width=0.5\textwidth]{blue.png}
  \caption{A picture of the universe!}
\end{figure}
\begin{figure}[h!]
\centering
  \includegraphics[width=0.5\textwidth]{blue.png}
  \caption{A picture of the universe!}
\end{figure}
\newpage

\begin{figure}[h!]
\centering
  \includegraphics[width=0.5\textwidth]{blue.png}
  \caption{A picture of the universe!}
\end{figure}

\newpage
\begin{figure}[h!]
\centering
  \includegraphics[width=0.5\textwidth]{blue.png}
  \caption{A picture of the universe!}
\end{figure}
\begin{figure}[h!]
\centering
  \includegraphics[width=0.5\textwidth]{blue.png}
  \caption{A picture of the universe!}
\end{figure}
\begin{figure}[h!]
\centering
  \includegraphics[width=0.5\textwidth]{blue.png}
  \caption{A picture of the universe!}
\end{figure}
\begin{figure}[h!]
\centering
  \includegraphics[width=0.5\textwidth]{blue.png}
  \caption{A picture of the universe!}
\end{figure}
\end{document}

希望这就是你要找的!(蓝色只是一张“绿盒子”

在此处输入图片描述

答案2

您需要\figurelabel在每个之后添加\caption。如果格式化每个图形的图形编号\begin{document}并使用全局宏存储它们。第一遍\thefigure将默认为\arabic{figure}。(不要弄乱图形计数器!)

\documentclass{article}
\usepackage{graphicx}
\usepackage{lipsum}% MWE only

\newcounter{figuresperpage}
\newcommand{\figurepage}{0}

\makeatletter
\newcommand{\figurelabel}{{\@bsphack
  \protected@write\@auxout{}{\string\newfigure{\arabic{figure}}{\thepage}}%
\@esphack}}

\newcommand{\newfigure}[2]{% #1 = \arabic{figure}, #2 = \thepage
  \ifnum\figurepage=#2\relax
    \stepcounter{figuresperpage}%
  \else
    \xdef\figurepage{#2}%
    \setcounter{figuresperpage}{1}%
  \fi
  \expandafter\xdef\csname figure#1\endcsname{#2.\thefiguresperpage}% store formatte figure number
}
\renewcommand{\thefigure}{\@ifundefined{figure\arabic{figure}}{\arabic{figure}}%
  {\csname figure\arabic{figure}\endcsname}}
\makeatother

\begin{document}
\listoffigures

\section{test}

\begin{figure}[p]
\centering
\includegraphics{example-image}
\caption{first}\figurelabel
\end{figure}

\begin{figure}[p]
\centering
\includegraphics{example-image}
\caption{second}\figurelabel
\end{figure}

\lipsum[1-12]   
\end{document}

相关内容