5

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.

  • 1
    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
  • 2
    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
  • 2
    @DaiBowen I will try working with such a solution like in http://tex.stackexchange.com/questions/21243/automatic-table-row-numbers and will update. – Konstantinos Aug 14 '16 at 16:34
  • 1
    Yep, I was thinking something like http://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
  • 1
    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
  • 1
    Please clarify if only instances of 1 & John should be modified, or if all instances of "any number followed by & John" -- e.g., "22 & John" -- should be modified. – Mico Aug 14 '16 at 17:26
  • 1
    Could you perhaps clarify the question, I've realised it's perhaps unclear whether you seek to make specific replacements or have some common increment over the tabulars in mind. – Dai Bowen Aug 14 '16 at 17:26
  • @Mico Thank you for your elaborate answer. I will also try to implement it, experiment with it and introduce myself to LuaTeX. "John" is not important, only the number is. So your current answer is more than enough. I am also in a kind of difficult position because I will most probably implement DaiBowen 's answer, because it is more specific and compact for my current needs. However, your answer seems what I had in mind when I was writing the question and it does answer to the title of the question and it's the most versatile as far as I can tell. Thanks once again. – Konstantinos Aug 14 '16 at 17:35
  • 1
    @pidosaurus - I'll provide an addendum to show how to change 1 to 11 if (and only if) it occurs in the first column of a tabular-like environment. – Mico Aug 14 '16 at 17:43

3 Answers3

6

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.

enter image description here

\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
luatexbase.add_to_callback ( "process_input_buffer" , 
   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.

Mico
  • 506,678
5

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

enter image description here

egreg
  • 1,121,712
3

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.

Dai Bowen
  • 6,117