# Can I use a function inside LaTeX to alter portions of \input{}?

I have a table in a LaTeX file, named tabular.tex which contains a tabular environment:

\begin{tabular}{rr}
Number & Name \\
1      & John \\
2      & Mary \\
\end{tabular}


and I input it in my main document with

\input{tabular.tex}


Can I somehow alter the Number, of say John, to make it 11 instead of 1 from within LaTeX? I could use sed or some other stream editor to make this change externally, but I would like to know if I could do this from within LaTeX.

The reason I ask this is that the actual tabular is huge and I would like to partially input lines, so in the end the lines would have consecutive numbering in my LaTeX main document.

• Does tabular.tex have to have the numbers 1, 2, ... or could the column be some macro in each line \printnextnum which can reference a counter defined in the main document? – Dai Bowen Aug 14 '16 at 16:25
• No. And "wow", I think I know where you are going with it. What I think you suggest is create the tabular in such a way so as the first field in each line is a counter, which is specific for this tabular (the document will have many such tabulars, so each one will have each one counter). – Konstantinos Aug 14 '16 at 16:33
• @DaiBowen I will try working with such a solution like in tex.stackexchange.com/questions/21243/… and will update. – Konstantinos Aug 14 '16 at 16:34
• Yep, I was thinking something like tex.stackexchange.com/a/21244/106162 but the first answer of the question you link to might be the more elegant. – Dai Bowen Aug 14 '16 at 16:39
• Thank you very much @DaiBowen I worked with the answer of Seamus, using the magicrownumbers counter. Just before each tabular environment I reset the counter with \setcounter{magicrownumbers}{0} and everything works fine for me! Thanks! – Konstantinos Aug 14 '16 at 16:48

If the numbers in the first column are hard-coded (1, 2, etc) and if you're free to use LuaLaTeX, it's straightforward to set up a Lua function that scans the input for 1 & John and, if found, replaces it with 11 & John. (I assume that instances of 11 & John and 21 & John should not be modified. Please advise if this assumption is invalid.) It's also straightforward to limit the operation of this function to tabular/tabular*/tabular[xy] environments.

\RequirePackage{filecontents}
\begin{filecontents*}{tabular.tex}
\begin{tabular}{rr}
Number & Name \\
\hline
1      & John \\
21     & John \\
2      & Mary \\
\end{tabular}
\end{filecontents*}

\documentclass{article}

\usepackage{luacode}
\begin{luacode*}
-- spring into action only inside a tabular environment
in_tabular = false
function change_john ( s )
if string.find ( s , "\\begin{tabular" ) then
in_tabular = true
elseif string.find ( s , "\\end{tabular" ) then
in_tabular = false
elseif in_tabular then
-- '%s-' denotes '0 or more instances of whitespace'
-- In the following code, we allow for up to three such
-- occurrences (before and after "1", and between "&" and "John")
return ( string.gsub ( s , "^%s-1%s-&%s-John" , "11 & John" ) )
end
end
change_john , "change_john" )
\end{luacode*}

\begin{document}
\input tabular \par
If not inside a \texttt{tabular} environment, \verb+1 & John+'' does \emph{not} get modified.
\end{document}


Addendum to address the OP's comment that "John" isn't relevant, only "1" is. If you need to change all instances of 1 (but not 11, 12, a.1 or 1.1) in the first column of a tabular-like environment, all you need to change in the Lua code is replace

     return ( string.gsub ( s , "^%s-1%s-&%s-John" , "11 & John" ) )


with

     return ( string.gsub ( s , "^%s-1%s-&" , "11 &" ) )


i.e., omit %s-John from the search string and John from the replacement string.

A (very brief!) Lua string function tutorial: ^ denotes the beginning of a line and %s- denotes "zero or more instances of whitespaces". As the Lua function change_john is assigned to LuaTeX's process_input_buffer callback, which operates at a very early stage, the string.gsub function replaces all matches of ^%s-1%s-& globally with "11 &", before TeX itself gets to do its usual work.

\patchcmd can help:

\begin{filecontents*}{\jobname-table.tex}
\begin{tabular}{rr}
Number & Name \\
1      & John \\
2      & Mary \\
\end{tabular}
\end{filecontents*}

\documentclass{article}
\usepackage{catchfile,etoolbox}

\begin{document}

\CatchFileDef{\mytabular}{\jobname-table.tex}{}
\patchcmd{\mytabular}{1 & John}{11 & John}{}{}
\mytabular

\end{document}


The filecontents* environment is used just for making the example self-contained.

If our tabular.tex file is to be used solely for the purpose of this latex, we could consider ourselves free to replace the numbering with a macro to extract the value of the current row counter (See Mico's answer for one way of dealing with such a case).

Since anything \input to a tex file is put in context of the rest of the file, the answers to Automatic table row numbers can also be used to answer this question.

In this scenario we use a tabular.tex of the form:

\begin{tabular}{rr}
Number & Name \\
\tablerownum & John \\
\tablerownum & Mary \\
\end{tabular}


While in the main document we must define the counter

\documentclass{article}

\newcounter{tablerowcounter}
\newcommand{\tablerownum}{\stepcounter{tablerowcounter}\arabic{tablerowcounter}}

\begin{document}
\setcounter{tablerowcounter}{10}
\input{tabular}
\end{document}


In order to deal with multiple tabulars (assuming no nesting or similar is going on) a second \input{tabulartwo} can be preceded to \setcounter{tablerowcounter}{1} to reset the counter as desired.