首页 > 解决方案 > LaTeX:无论源代码的主要块在哪里,是否可以将图像/表格放置在它们被引用的位置附近?

问题描述

我想知道是否可以将图像/表格放在靠近它们被引用的位置(它们对应\ref{images/tables}的位置),而不管这些图像/表格的主要源代码块在哪里?例如,我想将下面提到的表格放在与 where\ref{table:1}出现的同一页面中,但下面的代码在文档的最末尾,就在\end{document}.

\begin{table}[h!]
\centering
 \begin{tabular}{||c c c c||} 
 \hline
 Col1 & Col2 & Col2 & Col3 \\ [0.5ex] 
 \hline\hline
 1 & 6 & 87837 & 787 \\ 
 2 & 7 & 78 & 5415 \\
 3 & 545 & 778 & 7507 \\
 4 & 545 & 18744 & 7560 \\
 5 & 88 & 788 & 6344 \\ [1ex] 
 \hline
 \end{tabular}
 \caption{Table to test captions and labels}
 \label{table:1}
\end{table}

标签: latex

解决方案


通常最好将源放置在第一次引用浮动的地方。但是,如果您想在一个地方收集源代码(例如外部文件或序言),您可以执行以下操作:

\documentclass{article}

\usepackage{duckuments}

\newcommand{\taba}{%
\begin{table}[htbp]
\centering
 \begin{tabular}{||c c c c||} 
 \hline
 Col1 & Col2 & Col2 & Col3 \\ [0.5ex] 
 \hline\hline
 1 & 6 & 87837 & 787 \\ 
 2 & 7 & 78 & 5415 \\
 3 & 545 & 778 & 7507 \\
 4 & 545 & 18744 & 7560 \\
 5 & 88 & 788 & 6344 \\ [1ex] 
 \hline
 \end{tabular}
 \caption{Table to test captions and labels}
 \label{table:1}
\end{table}
}%

\begin{document}

\duckument

test \ref{table:1}\taba test

\duckument

\end{document}

(请不要h!用作浮动说明符。如果有的话,这将导致浮动的次优放置。让 TeX 尽其所能:为浮动找到最佳位置)


推荐阅读