# How to show column names of table from text file on the top of every page?

I have a table stored in text file. I want to insert the table into LaTeX and keep the column names appearing on the top of every page.

I tried the code as below, but it doesn't work. Could anybody give me some advice?

\usepackage{fancyvrb}
\begin{center}
\begin{longtable}{cccc}
1.id    2.Name   3.Age  4.gender\\[0.2cm]
\hline
\endhead % all the lines above this will be repeated on every page
\VerbatimInput[baselinestretch=1,fontsize=\\footnotesize]{myfile.txt}
\end{longtable}
\end{center}


myfile.txt is a 4-column table with hundreds of rows. Like

id      name              Age  gender
32432   Angela Cheung     34    F
23245   Katherine Zhang   25    F
254535  Tim Clark         42    M
2523    Richard Davis     44    M
435     Ed Pitt           23    M

-

## migrated from stackoverflow.comJun 7 '12 at 23:06

This question came from our site for professional and enthusiast programmers.

VerbatimInput isn't a standard latex command, please edit your post with a full (small)example so we can see what packages you are using, and what the myfile.txt looks like. (TeX tabular environments are delicate things and don't normally accept arbitrary input that is supposed to fill multiple cells. – David Carlisle Jun 7 '12 at 23:21
Welcome to TeX.sx! Your question was migrated here from Stack Overflow. Please register on this site, too, and make sure that both accounts are associated with each other (by using the same OpenID), otherwise you won't be able to comment on or accept answers or edit your question. – Werner Jun 7 '12 at 23:34

Here is an approach with LuaLaTeX. In the function readDataFile the content of the file is stored in a Lua table which is printed by using the function printTable. An issue is how to separate the column entries in the external file respectively how to split the string in the Lua function. In this example I used space as separator. So I get the name in two separate arguments which are joined in the LaTeX table (I added a column name in the textfile). I think it should be no problem to modifier the (simple) code to your needs.

\documentclass{book}
\usepackage{longtable}
\usepackage{filecontents}

%create a datafile
\begin{filecontents*}{myfile.txt}
ID Name LastName Age Gender
32432 Angela Cheung 34 F
23245 Katherine Zhang 25 F
254535 Tim Clark 42 M
2523 Richard Davis 44 M
435 Ed Pitt 23 M
\end{filecontents*}

%create a lua script file
\begin{filecontents*}{luaFunctions.lua}

 function readDataFile()

local input = io.open('myfile.txt', 'r')
peopleTable = {} --global table for storing the read values

for line in input:lines() do

--split the line with the delimiter
local split = string.explode(line, " ")

--save the arguments in variables
tableItem = {}
tableItem.Id = split[1]
tableItem.FirstName = split[2]
tableItem.LastName = split[3]
tableItem.Age = split[4]
tableItem.Gender = split[5]

--insert the arguments of one line in the table
table.insert(peopleTable, tableItem)
end

input:close()
end

function printTable()
tex.print(string.format("\\begin{longtable}{rlcc}"))

header = peopleTable[1] -- table header is stored in the first row
tex.print(string.format(" {%s} & {%s}  & {%s} & {%s}\\\\\\hline"

--create a latex string for every table entry (except the header)
for i,p in ipairs(peopleTable) do
if i~=1 then
tex.print(string.format(" {%s} & {%s} ".." ".." {%s} & {%s} & {%s}\\\\"
,p.Id, p.FirstName, p.LastName, p.Age, p.Gender))
end
end

tex.print(string.format("\\end{longtable}"))
end

\end{filecontents*}

% read the external lua file to declare the defined functions,
% but without execute the Lua commands and functions
\directlua{dofile("luaFunctions.lua")}

% latex commands to execute the lua functions