在 LaTeX 中创建细表

在 LaTeX 中创建细表

假设我有下表:

\begin{table}[!p]
\fontsize{6}{6}\selectfont
\caption{First 200 terms of Hofstadter's $Q(n)$}\label{tab11}
\center{
\begin{tabular}{l r r r r r l r r r r r}
    \hline
    {}&{}&{}&$n$&{}&{}&{}&{}&{}&$n$&{}&{}\\
    \cline{2-6}\cline{8-12}
    {}&1&2&3&4&5&{}&1&2&3&4&5\\
    \hline
    $Q(n+0)$&1&1&2&3&3&$Q(n+100)$&48&54&54&50&60\\
    $Q(n+5)$&4&5&5&6&6&$Q(n+105)$&52&54&58&60&53\\
    $Q(n+10)$&6&8&8&8&10&$Q(n+110)$&60&60&52&62&66\\
    $Q(n+15)$&9&10&11&11&12&$Q(n+115)$&55&62&68&62&58\\
    $Q(n+20)$&12&12&12&16&14&$Q(n+120)$&72&58&61&78&57\\
    $Q(n+25)$&14&16&16&16&16&$Q(n+125)$&71&68&64&63&73\\
    $Q(n+30)$&20&17&17&20&21&$Q(n+130)$&63&71&72&72&80\\
    $Q(n+35)$&19&20&22&21&22&$Q(n+135)$&61&71&77&65&80\\
    $Q(n+40)$&23&23&24&24&24&$Q(n+140)$&71&69&77&75&73\\
    $Q(n+45)$&24&24&32&24&25&$Q(n+145)$&77&79&76&80&79\\
    $Q(n+50)$&30&28&26&30&30&$Q(n+150)$&75&82&77&80&80\\
    $Q(n+55)$&28&32&30&32&32&$Q(n+155)$&78&83&83&78&85\\
    $Q(n+60)$&32&32&40&33&31&$Q(n+160)$&82&85&84&84&88\\
    $Q(n+65)$&38&35&33&39&40&$Q(n+165)$&83&87&88&87&86\\
    $Q(n+70)$&37&38&40&39&40&$Q(n+170)$&90&88&87&92&90\\
    $Q(n+75)$&39&42&40&41&43&$Q(n+175)$&91&92&92&94&92\\
    $Q(n+80)$&44&43&43&46&44&$Q(n+180)$&93&94&94&96&94\\
    $Q(n+85)$&45&47&47&46&48&$Q(n+185)$&96&96&96&96&96\\
    $Q(n+90)$&48&48&48&48&48&$Q(n+190)$&96&128&72&96&115\\
    $Q(n+95)$&64&41&52&54&56&$Q(n+195)$&100&84&114&110&93\\
    \hline
\end{tabular}
}

有没有办法让这个表格的每一行只出现一个序列数字?更好的办法是,有没有办法从 excel 文件中取出两列(一个计数列和一个包含递归项的列)并将它们转换为 LaTeX 友好格式?有程序可以做到这一点吗?谢谢!

编辑:本质上它应该是两列,包括标题在内有 31 行。我还没有决定确切的行数。我的想法是,左列为 n,右列为 Q(n),这样它看起来应该像这样:

 n Q(n)
 1  1
 2  1
 3  2
 4  3
 5  3
 6  4
 7  5

其中 Q(n) 的值与上面的值相同。

答案1

设置非常薄的表格的一种方法是使用以下方式循环浏览 CSVdatatool

在此处输入图片描述

\documentclass{article}
\usepackage[landscape,margin=1in]{geometry}% Just for this example

\usepackage{filecontents}
% https://oeis.org/A005185
\begin{filecontents*}{data.csv}
n, Q(n)
1, 1
2, 1
3, 2
4, 3
5, 3
6, 4
7, 5
8, 5
9, 6
10, 6
11, 6
12, 8
13, 8
14, 8
15, 10
16, 9
17, 10
18, 11
19, 11
20, 12
21, 12
22, 12
23, 12
24, 16
25, 14
26, 14
27, 16
28, 16
29, 16
30, 16
31, 20
32, 17
33, 17
34, 20
35, 21
36, 19
37, 20
38, 22
39, 21
40, 22
41, 23
42, 23
43, 24
44, 24
45, 24
46, 24
47, 24
48, 32
49, 24
50, 25
51, 30
52, 28
53, 26
54, 30
55, 30
56, 28
57, 32
58, 30
59, 32
60, 32
61, 32
62, 32
63, 40
64, 33
65, 31
66, 38
67, 35
68, 33
69, 39
70, 40
71, 37
72, 38
73, 40
74, 39
\end{filecontents*}

\usepackage{datatool,booktabs}
\newcounter{tablerow}
\begin{document}

\DTLloaddb[keys={SeqI,SeqVal}, headers={$n$, $Q(n)$}]{data}{data.csv}

\newcommand{\tabularcontents}{}
\makeatletter
\newcommand{\printtabularcontents}{{%
  \setlength{\tabcolsep}{.5\tabcolsep}% Make gap between columns 50% of the normal gap
  \begin{tabular}[t]{rr}
    \toprule
    \multicolumn{1}{c}{$n$} & \multicolumn{1}{c}{$Q(n)$} \\
    \midrule
    \tabularcontents
    \bottomrule
  \end{tabular}
}}
\newcommand{\tabulargap}{}%
\newcommand{\printdata}[1]{%
  \renewcommand{\tabulargap}{\renewcommand{\tabulargap}{\quad}}% https://tex.stackexchange.com/a/89187/5764
  \renewcommand{\tabularcontents}{}% Clear \tabularcontents
  \setcounter{tablerow}{0}% Restart row count
  \DTLforeach{data}{\SeqI=SeqI,\SeqVal=SeqVal}{%
    \protected@edef\tabularcontents{\tabularcontents \SeqI & \SeqVal \\}% Accumulate tabular contents
    \stepcounter{tablerow}% A new row
    \ifnum\value{tablerow}>\number\numexpr#1-1\relax
      \setcounter{tablerow}{0}%
      \printtabularcontents%
      \tabulargap% Space between 
      \renewcommand{\tabularcontents}{}% Clear built-up tabular contents
    \fi
  }%
  \if$\tabularcontents$\else
    \printtabularcontents%
  \fi
}
\makeatother

\footnotesize
\printdata{10}

\bigskip\bigskip

\printdata{7}

\end{document}

另一种方式是通过提供一份清单。有很多种方法可以遍历 CSV 列表

\documentclass{article}
\usepackage[landscape,margin=1in]{geometry}% Just for this example

\usepackage{etoolbox,booktabs}
\newcounter{tablerow}
\newcounter{listcount}
\begin{document}

\newcommand{\tabularcontents}{}
\makeatletter
\newcommand{\printtabularcontents}{{%
  \setlength{\tabcolsep}{.5\tabcolsep}% Make gap between columns 50% of the normal gap
  \begin{tabular}[t]{rr}
    \toprule
    \multicolumn{1}{c}{$n$} & \multicolumn{1}{c}{$Q(n)$} \\
    \midrule
    \tabularcontents
    \bottomrule
  \end{tabular}
}}
\newcommand{\tabulargap}{}
\newcommand{\printdata}[2]{%
  \renewcommand{\tabulargap}{\renewcommand{\tabulargap}{\quad}}% https://tex.stackexchange.com/a/89187/5764
  \renewcommand{\tabularcontents}{}% Clear \tabularcontents
  \setcounter{listcount}{0}\setcounter{tablerow}{0}% Restart row count
  \renewcommand*{\do}[1]{%
    \stepcounter{listcount}\stepcounter{tablerow}% A new row
    \protected@edef\tabularcontents{\tabularcontents \thelistcount & ##1 \\}% Accumulate tabular contents
    \ifnum\value{tablerow}>\number\numexpr#1-1\relax
      \setcounter{tablerow}{0}%
      \printtabularcontents%
      \tabulargap% Space between 
      \renewcommand{\tabularcontents}{}% Clear built-up tabular contents
    \fi
  }%
  \docsvlist{#2}% Process list
  \if$\tabularcontents$\else
    \printtabularcontents%
  \fi
}
\makeatother

\footnotesize
% https://oeis.org/A005185
\printdata{10}{1, 1, 2, 3, 3, 4, 5, 5, 6, 6, 6, 8, 8, 8, 
  10, 9, 10, 11, 11, 12, 12, 12, 12, 16, 14, 14, 16, 16, 
  16, 16, 20, 17, 17, 20, 21, 19, 20, 22, 21, 22, 23, 23, 
  24, 24, 24, 24, 24, 32, 24, 25, 30, 28, 26, 30, 30, 28, 
  32, 30, 32, 32, 32, 32, 40, 33, 31, 38, 35, 33, 39, 40, 
  37, 38, 40, 39}

\bigskip\bigskip

\printdata{7}{1, 1, 2, 3, 3, 4, 5, 5, 6, 6, 6, 8, 8, 8, 
  10, 9, 10, 11, 11, 12, 12, 12, 12, 16, 14, 14, 16, 16, 
  16, 16, 20, 17, 17, 20, 21, 19, 20, 22, 21, 22, 23, 23, 
  24, 24, 24, 24, 24, 32, 24, 25, 30, 28, 26, 30, 30, 28, 
  32, 30, 32, 32, 32, 32, 40, 33, 31, 38, 35, 33, 39, 40, 
  37, 38, 40, 39}

\end{document}

答案2

如果它仍然有用,并且因为在我看来它没有在该网络的其他地方被引用,Calc2LaTeX是一个用于将电子表格中的表格导入并转换为 LaTeX 的专用工具。

您可以轻松地准备表格的形状(行数和列数,以及单元格的一些格式)之内电子表格,转换,然后调整 LaTeX 标记以使输出适合您的口味或给定的规格。

我在一家出版公司工作时一直在使用 Calc2LaTeX:忘记跳过最后的(精细)调整,但最重要的是,如果您处理大型表格,它可以节省您的一些时间。

即使你最终使用longtable包/环境(而不是tabular默认的),Calc2LaTeX 也是一个很好的替代品全部替换手动大量的a1 & a2 & a3 & ... & an\\

相关内容