2

Is there a way to easily code from within (La)TeX some text manipulation stuff, like "find" and "replace", so that I may automatize tedious manual labor ?

(EDIT It was mentioned in the comments by Christian Hupfer that l3regex might be a solution, covering the constraints mentioned below - although David Carlisle said "no". Which one is it ?)

There are some important constraints though:

  • The approach has to be general, i.e. only using macros won't work, because I may want to do things like replace all occurences of "=" with ">".

  • I don't want to use LuaTeX (where I have heard that this can be done easily)

  • Coding should ideally by done in the preamble. As indicate here this can be done easily via the xstring package, but that has the disadvantage that if I want to search around the whole text (which can be quite large) I have to enclose everything in a \StrSubstitute[0] which seems an ugly approach and requires me to mess with the content of my document which I'd rather leave untouched.

  • At the very least I should be able to do text replacement, but I'd hope for some more advanced capabilities, meaning to have available at least a subset of the capabilities, regarding text manipulation, of a linux scripting language like sed

l7ll7
  • 2,267
  • 1
    How about regular expressions, from within the editor? – Bernard Apr 08 '16 at 18:20
  • 1
    Something like l3regex? –  Apr 08 '16 at 18:20
  • Can you give some usage cases, in terms of desired code? The context of what you are trying to achieve is not clear. – Steven B. Segletes Apr 08 '16 at 18:20
  • @Bernard The idea is to code one long chunk of code that I put in each of my documents that solves a whole bunch of problems. If I use regexp from the editor I assume I have to do a lot of clicking for each document to copy my script in it. – l7ll7 Apr 08 '16 at 18:23
  • @ChristianHupfer That certainly sounds interesting, I'll look into it! – l7ll7 Apr 08 '16 at 18:23
  • @StevenB.Segletes On instance: Replace all : with \colon. But the idea is for the language to be sufficiently powerful so that I keep my options open for the future and do not have to adopt a different solution, if some more complicated scenario of text replacement occurs. – l7ll7 Apr 08 '16 at 18:24
  • But do you mean in the form of a macro, as in \substring{\mystring}{:}{\colon}? – Steven B. Segletes Apr 08 '16 at 18:26
  • @StevenB.Segletes no, assume there are hundreds of ":" already spread throughout my document and I want to replace these with a single stroke. – l7ll7 Apr 08 '16 at 18:27
  • 1
    That clearly sounds like the function of an editor, not TeX. – Steven B. Segletes Apr 08 '16 at 18:28
  • @StevenB.Segletes Kind of, but imagine if I had to do that for 100 document or if the replacing is of a much more complicated nature that would usually, by hand require a lot of fiddling for a single instance of replacement. The l3regex package mentioned above seems to be exactly what I want, wouldn't you agree ? – l7ll7 Apr 08 '16 at 18:31
  • Any particular reason for not wanting to use Lua(La)TeX? :-) – Mico Apr 08 '16 at 18:40
  • 1
    No l3regex isn't really designed for whole document replacements, for the use of : in math mode you could just redefine its \mathcode to make it work like \colon without changing the token at all. – David Carlisle Apr 08 '16 at 18:40
  • @DavidCarlisle Apart from the fact that I might be "misusing" l3regex for something it has been not designed, Is there a reason why it won't work ? Perhaps its too slow for long documents ? – l7ll7 Apr 08 '16 at 18:45
  • For many documents in an automated way, I would think shell tools like sed and awk would be the right tools to build the substitution rules. – Steven B. Segletes Apr 08 '16 at 18:47
  • Not sure if we have the same standards for what constitutes a "very good tutorial," but I'd say it's worth reading (and re-reading...) A guide to LuaLATEX and The luacode package, both by Manuel Pégourié-Gonnard, to get a good introduction to Lua-in-LaTeX. – Mico Apr 08 '16 at 18:50
  • @Mico Both requir login credentials. Can you please provide easy accesible links ? – l7ll7 Apr 08 '16 at 18:53
  • @user10324 - I've updated the links. Hopefully, no further access issues... – Mico Apr 08 '16 at 18:55
  • Lua has a wonderful library of very powerful and flexible string manipulation functions. The entire string library is accessible to Lua(La)TeX users. Your requirement, "Coding should ideally by done in the preamble," is straightforward to implement. – Mico Apr 08 '16 at 18:59
  • @user10324: I did not say that l3regex is meant for whole documents –  Apr 08 '16 at 20:23
  • @ChristianHupfer Could you please explain ? – l7ll7 Apr 13 '16 at 11:04
  • @user10324: What shall I explain? –  Apr 13 '16 at 13:00
  • @ChristianHupfer Your previous comment: Why l3regex is not meant for whole documents. – l7ll7 Apr 13 '16 at 14:58
  • @user10324: David Carlisle explained this already. If I had known hat you want to manipulate the whole document, I would not have suggested l3regex –  Apr 13 '16 at 15:07

1 Answers1

2

You've stated that you "may want to do things like replace all occurrences of = with >" and also that "[c]oding should ideally by done in the preamble".

I'm going to keep my fingers crossed that you'll reconsider the decision not to use LuaLaTeX. Lua (the programming language) has a very flexible and powerful string library, and LuaTeX offers several ways to assign Lua-coded functions to various "callbacks" -- meeting your requirement that the coding should be all done in the preamble. In the following example, the function eq2gt (which, as its name suggests, replaces all instances of = with >) is assigned to the process_input_buffer callback, which operates at a very early stage of processing, viz., before TeX's "eyes" start their processing. That way, the eq2gt function can act as a pre-processor, modifying parts of the input file "on the fly" before the typesetting job itself commences.

% !TEX TS-program = lualatex
\documentclass{article}

%% Lua-side code
\usepackage{luacode}
\begin{luacode}
function eq2gt ( buff )
   return ( string.gsub ( buff , "=" , ">" ) )
end
\end{luacode}

%% TeX-side code
\AtBeginDocument{\luadirect{luatexbase.add_to_callback(
   "process_input_buffer" , eq2gt , "eq2gt" )}}

\begin{document} 
\[
1+1+1=2    % not correct...
\]

$1-1-1=-2$ % not correct either...
\end{document}
Mico
  • 506,678
  • 1
    +1, because, this is a good answer, enticing me to use LuaTex. But I'm not going to accept it yet, as I haven't yet been able to make the move to LuaTex (which will be a big move, as I already have my system setup just the way I want; so I hope you take no offense if I wait a bit around to see if also an l3regex answer comes up). – l7ll7 Apr 11 '16 at 18:57