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
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.
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?magicrownumbers
counter. Just before each tabular environment I reset the counter with\setcounter{magicrownumbers}{0}
and everything works fine for me! Thanks!