将表格/图形的定义与正文分开

将表格/图形的定义与正文分开

免责声明:这不是关于排版过程中表格/图形放置的微观管理的问题,而是关于原始乳胶文件本身的组织的问题。

序言:在编写大量乳胶文档时,我觉得将表格/图形/算法/...的定义混合在文本正文中通常会导致混乱。在编写/校对原始乳胶文档(不是排版文件)时,当文本被包含制表结果或图形所需的众多命令所稀释时(特别是对于非常复杂的表格),很难专注于科学内容。

为了解决这个问题,我最终将表格/图形的定义封装在\newcommand元素中。这样,我的所有表格/图形都分组在文档的开头,然后更有意义的文本作为一个整体出现。以下 MWE 给出了一个想法:

\documentclass{article}
\usepackage{graphicx}

\begin{document}

\newcommand{\plugFigureApple}{
    \begin{figure}[h]
    \includegraphics{foo}
    \caption{This is the caption for the apple figure.}
    \label{fig:apple}
    \end{figure}
}

\newcommand{\plugFigureOrange}{
    \begin{figure}[h]
    \includegraphics{bar}
    \caption{This is the caption for the orange figure.}
    \label{fig:orange}
    \end{figure}
}

\newcommand{\plugTableFruits}{
    \begin{table}[h]
    \caption{This is the caption for the fruits table.}
    \label{tab:fruits}
    \begin{tabular}{ l | c }
    Fruit & Calories \\
    \hline
    Apple  & 52 \\
    Orange & 47 \\
    \end{tabular}
    \end{table}
}

This is a paragraph where apples are presented in Figure~\ref{fig:apple}. This is a very serious discussion regarding apples.
\plugFigureApple{}

This is a paragraph where oranges are presented in Figure~\ref{fig:orange}. This is a quite a comprehensive analysis.
\plugFigureOrange{}

This is one final paragraph where fruits are discussed in Table~\ref{tab:fruits}. This is a thorough discussion.
\plugTableFruits{}

\end{document}

这有效地允许将注意力集中在文本上,而不会受到太多干扰。但由于某些文件包含大量表格和图形,向下滚动以找到第一个有意义的句子现在变得不切实际。理想情况下,我希望真正的文本首先出现,以便表格/图形定义最终位于 latex 文件的底部。这不是使用我的提议的选项,因为\newcommand必须事先定义元素。

所以我的问题是:有没有一种方法(或一个包)可以让我从正文开始,到文件底部的所有表格/图形定义结束,同时在排版过程中对这些表格/图形的位置进行某种控制(因为我不希望它们只是作为列表出现在文档末尾)。我不介意手动提供一些位置标记(例如\plugFigureApple{}上面的 MWE 中的),但表格/图形也可以自动放置在离它们各自的第一个参考尽可能近的地方。

答案1

\documentclass{article}
\usepackage{graphicx}
\usepackage{filecontents}

\begin{document}

\newcommand\inputfloat[1]{\InputIfFileExists{float-#1}{}{\typeout{skipping #1}}}


This is a paragraph where apples are presented in Figure~\ref{fig:apple}. This is a very serious discussion regarding apples.
\inputfloat{apple}

This is a paragraph where oranges are presented in Figure~\ref{fig:orange}. This is a quite a comprehensive analysis.
\inputfloat{orange}

This is one final paragraph where fruits are discussed in Table~\ref{tab:fruits}. This is a thorough discussion.
\inputfloat{fruits}


\begin{filecontents}{float-apple}
  \begin{figure}[htp]
    \rule{1cm}{2cm}%\includegraphics{foo}
    \caption{This is the caption for the apple figure.}
    \label{fig:apple}
    \end{figure}
\end{filecontents}

\begin{filecontents}{float-orange}
    \begin{figure}[htp]
    \rule{1cm}{2cm}%\includegraphics{bar}
    \caption{This is the caption for the orange figure.}
    \label{fig:orange}
    \end{figure}
\end{filecontents}

\begin{filecontents}{float-fruits}
    \begin{table}[htp]
    \caption{This is the caption for the fruits table.}
    \label{tab:fruits}
    \begin{tabular}{ l | c }
    Fruit & Calories \\
    \hline
    Apple  & 52 \\
    Orange & 47 \\
    \end{tabular}
    \end{table}
\end{filecontents}


\end{document}

相关内容