![我如何计算图形使用的总空间?](https://linux22.com/image/408185/%E6%88%91%E5%A6%82%E4%BD%95%E8%AE%A1%E7%AE%97%E5%9B%BE%E5%BD%A2%E4%BD%BF%E7%94%A8%E7%9A%84%E6%80%BB%E7%A9%BA%E9%97%B4%EF%BC%9F.png)
我即将完成我的博士论文,我需要指定论文的一些技术特征,包括所有图表使用的累计空间(以页数为单位)。是否有任何神奇的函数可以告诉我这个页数?还是我应该用眼睛测量?
答案1
这是通过修补\end@float
图形等所使用的命令来实现的。
\documentclass{article}
\usepackage{etoolbox}
\usepackage{pgfmath}
\newlength{\sumfloatht}
\setlength{\sumfloatht}{0pt}
\makeatletter
\patchcmd{\end@float}{\@endfloatbox}{\@endfloatbox
\global\advance\sumfloatht by \ht\@currbox}{}{}
\AtEndDocument{\pgfmathdivide{\sumfloatht}{\textheight}%
\edef\@currentlabel{\pgfmathresult}%
\label{totalpages}}
\makeatother
\newcommand{\totalpages}{\pgfmathdivide{\sumfloatht}{\textheight}%
\pgfmathresult}
\begin{document}
\ref{totalpages} pages.
\begin{figure}[t]
\rule{\textwidth}{200pt}
\end{figure}
\begin{figure}[h]
\rule{\textwidth}{200pt}
\end{figure}
\begin{figure}[bp]
\rule{\textwidth}{200pt}
\end{figure}
{\the\sumfloatht} total height.
{\totalpages} pages.
\end{document}
此版本区分了图形和表格。要添加算法浮点数(例如),只需创建\sumalgorithmht
长度并将\label
等添加到\AtEndDocument
。
\documentclass{article}
\usepackage{etoolbox}
\usepackage{pgfmath}
\newlength{\sumfigureht}\setlength{\sumfigureht}{0pt}
\newlength{\sumtableht}\setlength{\sumtableht}{0pt}
\newlength{\sumfloatht}\setlength{\sumfloatht}{0pt}% anything else
\makeatletter
\patchcmd{\end@float}{\@endfloatbox}{\@endfloatbox
\@ifundefined{sum\@captype ht}{\global\advance\sumfloatht by \ht\@currbox}%
{\global\expandafter\advance\csname sum\@captype ht\endcsname by \ht\@currbox}%
}{}{}
\AtEndDocument{\pgfmathdivide{\sumfigureht}{\textheight}%
\edef\@currentlabel{\pgfmathresult}%
\label{figurepages}%
\pgfmathdivide{\sumtableht}{\textheight}%
\edef\@currentlabel{\pgfmathresult}%
\label{tablepages}%
\pgfmathdivide{\sumfloatht}{\textheight}%
\edef\@currentlabel{\pgfmathresult}%
\label{floatpages}%
}
\makeatother
\begin{document}
\ref{figurepages} figure pages.
\ref{tablepages} table pages.
\ref{floatpages} anything else.
\begin{figure}[h]
\rule{\textwidth}{0.2\textheight}
\end{figure}
\begin{table}[h]
\rule{\textwidth}{0.3\textheight}
\end{table}
\end{document}
答案2
请向 John Kormylo 提供解决方案表示感谢!我只是展示了一个方便的解决方法,使用包将此值包含在文档的任何位置totcount
(需要运行两次)。
\documentclass{article}
\usepackage{etoolbox}
\usepackage{pgfmath}
\usepackage{totcount}
\makeatletter
\AtBeginDocument{%
\newlength{\sumfloatheight@new}%
\patchcmd{\end@float}{\@endfloatbox}{\@endfloatbox \global\advance\sumfloatheight@new by \ht\@currbox}{}{}%
\newtotcounter{sumfloatheight@old}%
\newlength{\sumfloatheight@old}%
\setlength{\sumfloatheight@old}{\totvalue{sumfloatheight@old}sp}%
\newcommand{\totalfigurepages}{\pgfmathdivide{\sumfloatheight@old}{\textheight}\pgfmathresult}%
}%
\AtEndDocument{%
\setcounter{sumfloatheight@old}{\sumfloatheight@new}%
}%
\makeatother
\begin{document}
\totalfigurepages{} pages.
\begin{figure}[t]
\rule{\textwidth}{200pt}
\end{figure}
\begin{figure}[h]
\rule{\textwidth}{200pt}
\end{figure}
\begin{figure}[bp]
\rule{\textwidth}{200pt}
\end{figure}
\totalfigurepages{} pages.
\end{document}