TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I would like to replace some text (no commands) which are included in my tex-file with some certain string. This shall happen (if possible) without modifying the actual source file. Therefore, I don't want to use some bash/shell script or text-editor replacing function.

Is there any command/toolbox which does this? I have seen that there is an package called l3regex, unfortunately I don't know how to use the tex commands and I don't even know whether this a suitable package for this.

Sorry for being imprecise. Here an example.



This here is very long text which also has some formulas $a_{ij} = b*x^2$.


And here, I e.g. want to replace using a regular expression a_{ij} by X^{ij}. Or maybe some other text in the included files.

share|improve this question
Is conditional compilation an option? By this, I mean, extra lines of document that will or will not be executed depending on the state of a variable. – Steven B. Segletes Feb 7 '14 at 16:49
Please provide a small example document with an indication of the needed replacements. In particular where is the text? in headings? in verbatim sections? in one specific environment? the details will affect the possible answers. – David Carlisle Feb 7 '14 at 16:49
@DavidCarlisle sorry, I edited the question. – bonanza Feb 7 '14 at 16:56

l3regex can apply a regular expression to a supplied list of tokens but the harder part is to reliably apply any operation to text occurring freely in the document rather than text that is the argument to some command.

It is possible in simple cases but likely to be incompatible with packages used in real documents.

So, despite the fact that you say you don't want a bash script, I offer this bash script, it does not edit (or even save) the modified document, just pipes it into latex.




This here is very long text which also has some formulas $a_{ij} = b*x^2$.

second para.


The command line

 sed -e "s/a_{ij}/x^{ij}/g" regex.tex  | pdflatex  --jobname=regex \\relax


enter image description here

share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.