5

Suppose I have the following table:

\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}
}

Is there a way to take this table and make it so that only one number of the sequence occurs on each line? Better yet, is there a way to take two columns from an excel file (a counting column and a column containing terms of the recursion) and transform them to a LaTeX friendly format? Are there programs that do this? Thanks!

Edit: In essence it should be two columns and, say, 31 rows including headers. The exact number of rows is something I haven't actually decided on. The idea is there would be a left column, n, and a right column Q(n) so that it would look something like:

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

Where the values for Q(n) are the same as the values above.

David Carlisle
  • 757,742
user33916
  • 83
  • 3
  • 1
    Several posts (e.g., http://tex.stackexchange.com/questions/124261/import-r-data-frame-into-org-mode-table/124280#124280) talk about importing data from a file and formatting it for tabular output – Steven B. Segletes Jul 25 '13 at 20:43
  • 4
    Your question is not very clear; can you sketch how the table should become? – egreg Jul 25 '13 at 20:46
  • @StevenB You're right, I will definitely take a look at those. – user33916 Jul 25 '13 at 21:06
  • @egreg Sorry about that. In essence it should be two columns and, say, 31 rows including headers. – user33916 Jul 25 '13 at 21:07
  • @user33916 What would be the header and the first couple of rows? Please, add to your question, rather than in comments – egreg Jul 25 '13 at 21:09
  • See the following question, it might be of help: multicolumn long table or enumeration – Alan Munn Jul 25 '13 at 21:27
  • 1
    Are you trying to convert from LaTeX (10 columns) to Excel (2 columns), or the opposite? You seem to be mixing both in your question. If from Excel to LaTeX, you could easily write Excel code with a vlookup to get what you want: I've posted an example Excel file here – Xavier Jul 26 '13 at 19:28
  • Please, always post complete code. That is much more useful than mere fragments. I'd transform it with sed. If it started in Excel, I'd export to .csv and then I'd transform it with sed. – cfr Apr 17 '14 at 23:58

2 Answers2

4

One way of setting very thin tables is to cycle through a CSV using datatool:

enter image description here

\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}

Another way of doing this would be through supplying of a list. There are many ways of iterating through a CSV list:

\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}
Werner
  • 603,163
2

In case it may still be useful, and because it seems to me that it is not cited elsewhere on this network, Calc2LaTeX is a specific tool to import and convert tables from spreadsheet to LaTeX.

Easily, you'll prepare the shape of your table (number of lines and columns, some formatting of cells) within the spreadsheet, convert, and then adjust LaTeX markup to make the output suitable to your taste or given specifications.

I've been using Calc2LaTeX when I was working in a pubblishing house: forget to skip the final (fine) tuning but, above all if you handle large tables, it can save some of your time.

Even if you'll end up to use longtable package/environment (instead of tabular or default), Calc2LaTeX is a good alternative to replace all manually large amounts of a1 & a2 & a3 & ... & an\\.

MattAllegro
  • 1,546