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.

  • 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. Feb 7, 2014 at 16:49
  • 1
    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. Feb 7, 2014 at 16:49
  • @DavidCarlisle sorry, I edited the question.
    – bonanza
    Feb 7, 2014 at 16:56

1 Answer 1


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

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .