我目前正在尝试编写一些文本,其中包含表格以显示我需要显示的数据,并且其中还有一个 \lstinputlisting,用于加载我想要包含的外部 .txt 文档。令人讨厌的是,表格往往会聚集在距离相应文本太远的某些页面上。我试图通过在所有表格中使用 [h] 来解决这个问题,但这会导致 \lstinputlisting 出现问题,因为现在 .txt 文档在表格之间断开,看起来非常难看。有没有办法不使用 \lstinputlisting 作为浮点数,但仍防止它在表格上断开,但在分页符上继续断开?或者甚至将其作为浮点数,但在分页符上继续断开(因为它超过 1 页)。
这可以作为 MWE 发挥作用吗?
\documentclass{article}
\usepackage{floatrow}
\usepackage{lipsum}
\usepackage{filecontents}
\usepackage{listings}
\begin{document}
\begin{table}[h]
\begin{tabular}{lllllll}
0 & 0 & 0 & 0 & 0 & 0 & 0 \\
0 & 0 & 0 & 0 & 0 & 0 & 0 \\
0 & 0 & 0 & 0 & 0 & 0 & 0 \\
0 & 0 & 0 & 0 & 0 & 0 & 0 \\
0 & 0 & 0 & 0 & 0 & 0 & 0 \\
0 & 0 & 0 & 0 & 0 & 0 & 0 \\
0 & 0 & 0 & 0 & 0 & 0 & 0
\end{tabular}
\end{table}
\begin{table}[h]
\begin{tabular}{lllllll}
0 & 0 & 0 & 0 & 0 & 0 & 0 \\
0 & 0 & 0 & 0 & 0 & 0 & 0 \\
0 & 0 & 0 & 0 & 0 & 0 & 0 \\
0 & 0 & 0 & 0 & 0 & 0 & 0 \\
0 & 0 & 0 & 0 & 0 & 0 & 0 \\
0 & 0 & 0 & 0 & 0 & 0 & 0 \\
0 & 0 & 0 & 0 & 0 & 0 & 0
\end{tabular}
\end{table}
\lipsum[1-4]
\begin{table}[h]
\begin{tabular}{lllllll}
0 & 0 & 0 & 0 & 0 & 0 & 0 \\
0 & 0 & 0 & 0 & 0 & 0 & 0 \\
0 & 0 & 0 & 0 & 0 & 0 & 0 \\
0 & 0 & 0 & 0 & 0 & 0 & 0 \\
0 & 0 & 0 & 0 & 0 & 0 & 0 \\
0 & 0 & 0 & 0 & 0 & 0 & 0 \\
0 & 0 & 0 & 0 & 0 & 0 & 0
\end{tabular}
\end{table}
\begin{table}[h]
\begin{tabular}{lllllll}
0 & 0 & 0 & 0 & 0 & 0 & 0 \\
0 & 0 & 0 & 0 & 0 & 0 & 0 \\
0 & 0 & 0 & 0 & 0 & 0 & 0 \\
0 & 0 & 0 & 0 & 0 & 0 & 0 \\
0 & 0 & 0 & 0 & 0 & 0 & 0 \\
0 & 0 & 0 & 0 & 0 & 0 & 0 \\
0 & 0 & 0 & 0 & 0 & 0 & 0
\end{tabular}
\end{table}
\begin{table}[h]
\begin{tabular}{lllllll}
0 & 0 & 0 & 0 & 0 & 0 & 0 \\
0 & 0 & 0 & 0 & 0 & 0 & 0 \\
0 & 0 & 0 & 0 & 0 & 0 & 0 \\
0 & 0 & 0 & 0 & 0 & 0 & 0 \\
0 & 0 & 0 & 0 & 0 & 0 & 0 \\
0 & 0 & 0 & 0 & 0 & 0 & 0 \\
0 & 0 & 0 & 0 & 0 & 0 & 0
\end{tabular}
\end{table}
\begin{table}[h]
\begin{tabular}{lllllll}
0 & 0 & 0 & 0 & 0 & 0 & 0 \\
0 & 0 & 0 & 0 & 0 & 0 & 0 \\
0 & 0 & 0 & 0 & 0 & 0 & 0 \\
0 & 0 & 0 & 0 & 0 & 0 & 0 \\
0 & 0 & 0 & 0 & 0 & 0 & 0 \\
0 & 0 & 0 & 0 & 0 & 0 & 0 \\
0 & 0 & 0 & 0 & 0 & 0 & 0
\end{tabular}
\end{table}
\begin{filecontents*}{portableData.txt}
foo
bar
baz
foo
foo\\
foofoo\\
foo\\
foo\\
foo\\
foo\\
foofoo\\
foo\\
foofoo\\
foo\\
foofoo\\
foo\\
foofoo\\
foo\\
foofoo\\
foofoo\\
foo\\
foofoo\\
foo\\
foofoo\\
foofoo\\
foo\\
foofoo\\
foo\\
foo\\
foofoo\\
foofoo\\
foo\\
foofoo\\
foo\\
foofoo\\
foo\\
foo\\
foofoo\\
foo\\
foofoo\\
\\
foofoo\\
foo\\
foofoo\\
foo\\
foofoo\\
foo\\
foofoo\\
foo\\
foo\\
foofoo\\
foo\\
foofoo\\
foo\\
foo\\
foofoo\\
foo\\
foofoo\\
\end{filecontents*}
\lstinputlisting{portableData.txt}
\end{document}
非常感谢您的帮助。
答案1
我建议使用 placeins 包中的 \FloatBarrier 以及更宽松的浮点参数(https://texfaq.org/FAQ-floats):
\documentclass{article}
\usepackage{floatrow}
\usepackage{lipsum}
\usepackage{filecontents}
\usepackage{listings}
\usepackage{placeins}
\renewcommand{\topfraction}{.85}
\renewcommand{\bottomfraction}{.7}
\renewcommand{\textfraction}{.15}
\renewcommand{\floatpagefraction}{.66}
\renewcommand{\dbltopfraction}{.66}
\renewcommand{\dblfloatpagefraction}{.66}
\setcounter{topnumber}{9}
\setcounter{bottomnumber}{9}
\setcounter{totalnumber}{20}
\setcounter{dbltopnumber}{9}
\begin{document}
\begin{table}
\begin{tabular}{lllllll}
0 & 0 & 0 & 0 & 0 & 0 & 0 \\
0 & 0 & 0 & 0 & 0 & 0 & 0 \\
0 & 0 & 0 & 0 & 0 & 0 & 0 \\
0 & 0 & 0 & 0 & 0 & 0 & 0 \\
0 & 0 & 0 & 0 & 0 & 0 & 0 \\
0 & 0 & 0 & 0 & 0 & 0 & 0 \\
0 & 0 & 0 & 0 & 0 & 0 & 0
\end{tabular}
\end{table}
\begin{table}
\begin{tabular}{lllllll}
0 & 0 & 0 & 0 & 0 & 0 & 0 \\
0 & 0 & 0 & 0 & 0 & 0 & 0 \\
0 & 0 & 0 & 0 & 0 & 0 & 0 \\
0 & 0 & 0 & 0 & 0 & 0 & 0 \\
0 & 0 & 0 & 0 & 0 & 0 & 0 \\
0 & 0 & 0 & 0 & 0 & 0 & 0 \\
0 & 0 & 0 & 0 & 0 & 0 & 0
\end{tabular}
\end{table}
\lipsum[1-4]
\begin{table}
\begin{tabular}{lllllll}
0 & 0 & 0 & 0 & 0 & 0 & 0 \\
0 & 0 & 0 & 0 & 0 & 0 & 0 \\
0 & 0 & 0 & 0 & 0 & 0 & 0 \\
0 & 0 & 0 & 0 & 0 & 0 & 0 \\
0 & 0 & 0 & 0 & 0 & 0 & 0 \\
0 & 0 & 0 & 0 & 0 & 0 & 0 \\
0 & 0 & 0 & 0 & 0 & 0 & 0
\end{tabular}
\end{table}
\begin{table}
\begin{tabular}{lllllll}
0 & 0 & 0 & 0 & 0 & 0 & 0 \\
0 & 0 & 0 & 0 & 0 & 0 & 0 \\
0 & 0 & 0 & 0 & 0 & 0 & 0 \\
0 & 0 & 0 & 0 & 0 & 0 & 0 \\
0 & 0 & 0 & 0 & 0 & 0 & 0 \\
0 & 0 & 0 & 0 & 0 & 0 & 0 \\
0 & 0 & 0 & 0 & 0 & 0 & 0
\end{tabular}
\end{table}
\begin{table}
\begin{tabular}{lllllll}
0 & 0 & 0 & 0 & 0 & 0 & 0 \\
0 & 0 & 0 & 0 & 0 & 0 & 0 \\
0 & 0 & 0 & 0 & 0 & 0 & 0 \\
0 & 0 & 0 & 0 & 0 & 0 & 0 \\
0 & 0 & 0 & 0 & 0 & 0 & 0 \\
0 & 0 & 0 & 0 & 0 & 0 & 0 \\
0 & 0 & 0 & 0 & 0 & 0 & 0
\end{tabular}
\end{table}
\begin{table}
\begin{tabular}{lllllll}
0 & 0 & 0 & 0 & 0 & 0 & 0 \\
0 & 0 & 0 & 0 & 0 & 0 & 0 \\
0 & 0 & 0 & 0 & 0 & 0 & 0 \\
0 & 0 & 0 & 0 & 0 & 0 & 0 \\
0 & 0 & 0 & 0 & 0 & 0 & 0 \\
0 & 0 & 0 & 0 & 0 & 0 & 0 \\
0 & 0 & 0 & 0 & 0 & 0 & 0
\end{tabular}
\end{table}
\FloatBarrier
\begin{filecontents*}{portableData.txt}
foo
bar
baz
foo
foo\\
foofoo\\
foo\\
foo\\
foo\\
foo\\
foofoo\\
foo\\
foofoo\\
foo\\
foofoo\\
foo\\
foofoo\\
foo\\
foofoo\\
foofoo\\
foo\\
foofoo\\
foo\\
foofoo\\
foofoo\\
foo\\
foofoo\\
foo\\
foo\\
foofoo\\
foofoo\\
foo\\
foofoo\\
foo\\
foofoo\\
foo\\
foo\\
foofoo\\
foo\\
foofoo\\
\\
foofoo\\
foo\\
foofoo\\
foo\\
foofoo\\
foo\\
foofoo\\
foo\\
foo\\
foofoo\\
foo\\
foofoo\\
foo\\
foo\\
foofoo\\
foo\\
foofoo\\
\end{filecontents*}
\lstinputlisting{portableData.txt}
\end{document}