In an earlier post, we talked about the perks of using search and replace wisely on XML documents. Here I want to argue that search and replace might be even more powerful when used in LaTeX.
The power of search and replace
Imagine your thesis advisor decides last minute that they don’t like the way images are typeset in your thesis. It happens. We are speaking from our friend the Noob’s experience here. So imagine your advisor decides the images in your archaeological catalogue are too small, at the very last minute. In Microsoft Word, problem No. 1 you would have already encountered before now is that the document probably wouldn’t even open anymore (what with 200 pages full of images etc.). The second problem would be that you’d have to change each image size by hand, clicking and dragging around, cleaning up after the horrible mess and destruction your changes are leaving behind. In LaTeX, thanks goodness we can just search and replace. Imagine you’d had your image size set to 0.2/textwidth
then you could just open your catalogue.tex
and globally search and replace this value (because we’re hoping you didn’t have anything else in there which could accidentally found and replaced).
That’s what leads already us to the next point:
Damage control
When using global search and replace or ‘replace all’ (or whatever it is you’re doing) never do ‘replace all’ without jumping through a few items to check that you’re really only finding what you were expecting to find. And be wary of replacing short strings. I always try to find the longest possible string even when in the end, the difference between the searched and the replaced string is only one letter. So, for example, when changing something about images, like in the example above, be sure to search for the whole, or a as long as possible substring of, the includegraphics
.
Go on until the point where the differing arguments start (such as image file names or options – in this case I’m assuming you don’t have many more differing options per image). Should this be the case, don’t panic, witty search expression choices can get us around that one as well.
In these cases you might want to try a multi-step approach: For example, first look for the front part of includegraphics
before the first option, adding the 0.3\textwidth
(in case the option you want to change is further back, but lost in a mess of other options which can’t be globally searched and replaced). Now you have two image width statements. Then you search for your old expression, 0.2\textwidth
, and replace it by nothing to delete it. However, this can only be done wiith ‘replace all’ assuming you don’t use this exact string anywhere else in your document where it should stay. If this is the case, you can still search and replace but step through every find to check before replacing. This still usually saves you a lot of time.
If you realize a text you have copied and pasted in your document from OCR has square brackets which mess up your document, for the love of God, do not – I repeat – do not use ‘search and replace all’ to make them into \rbrack{}
and \lbrack{}
respectively. Check first whether your document uses these in arguments, too, where they are supposed to remain ‘litteral’ and are not supposed to be replaced by \rbrack{}
.
So the first thing to remember is: Be very careful when replacing protected entities in LaTeX.
For texts where you want to do lots of search and replace, I would generally recommend you take the affected text part out into a temporary search and replace file, using \include{temp}
. If you just want to search and replace in the text body, not the preamble, just take the text out to a different document like this. Then you don’t need to worry about accidentally replacing half of the \usepackages
or whatever.
Use cases
The first one (changing image sizes globally in a document with tons of images), we have already seen in the intro. But what else can you do with this? Stay tuned for the next post in this series ‘Strategically Using Search-And-Replace for LaTeX documents’ to learn about going:
- From abstract to slides to poster
- Switching between environments like between paracol and minipage
- from
minipage
with image to wrapfigure - plus a few fun bonus steps
- and you’ll get an extra post on the probably most wanted and requested hot topic of automatically generating slides from your LaTeX documents.
Stay tuned!
Conclusion
Most LaTeX editors have a search and replace function. If you have regex as well, that’s an even mightier tool you can put to use. But you don’t really need it. You’ll have more than enough power to yield with those new tools I just handed you (and you’ll heap up if you follow up with the other posts of this series as well). I would have included the probably most obvious use case, getting slides from ‘normal’ documents, already but then I thought it deserved a post of it’s own.
When adapting the methods presented here to your own use cases, always remember to:
- Search and replace the longest possible string,
- Do the replacements in a separete document which is included into the rest
- Make sure
\
and curly brackets match up in the search and the replace field: if you search for\command
, also replace to\newcommand
, but when\test{
, then\new{
, etc.
Maybe you were aware that you can do search and replace in your editor but you never knew how to exploit its power. Now you do. So search and replace along happily.
Best,
the Ninja
PS: Were you aware of these tricks? Were you already using them? What are your favourite tricks? Let me know in the comments!
Buy me coffee!
If my content has helped you, donate 3€ to buy me coffee. Thanks a lot, I appreciate it!
€3.00
One thought on “Strategically Using Search-And-Replace for LaTeX documents. Part I”