1

Is there a script out there to expand LaTeX commands in a tex source?

I am submitting a manuscript source and I need a tool that expands a LaTeX command because I do not want some commands to appear in the source. Example source:

\documentclass{article}
\newcommand{\modified}[2]{#2}
\begin{document}
    \modified{Old long
     % Possible comments 
    Text} {New
    correct 
    Text % with comments
    }
\end{document}

After executing the command, I would like an output like this

\documentclass{article}
\newcommand{\modified}[2]{#2}
\begin{document}
    New
    correct 
    Text % with comments
\end{document}

If such a script doesn't exist, can you suggest tools to write one? For example, is there a Perl package that is LaTeX-aware?

  • 1
    Just to be clear: You're looking for a tool that modifies the input file, right? – Mico Apr 1 '16 at 8:20
  • 2
    Well, this is rather a task for Perl or sed, in my point of view – user31729 Apr 1 '16 at 8:38
  • Yes, the tool would modify the input file. – Tohiko Apr 1 '16 at 10:33
1

As far as I know there is no such tool. A generic TeX macro expander would expand all macros unless it encounters something unexpandable (usually a TeX primitive or input tokens). This kind of output would be plain unreadable.

So I suggest that you write your command replacement tool yourself in a script language you know well (perl and sed are good suggestions for this task, python and emacs-lisp come also to my mind).

  • I am more thinking of a tool that would take a list of commands and expand only those. I will probably write it myself – Tohiko Apr 1 '16 at 10:47
1

Your MWE suggests that your \modified macro is here to hide former versions.

Then a very simple approach is simply to comment out the old versions in lines starting with a distinctive pattern, say

%private

or

%!!

Then use sed utility for example to remove all private lines, something like

sed '/^%!!/d' foo.tex > bar.tex

You can also do something like \catcode1 14 and use ^^A at the start of the private lines then

sed '/^^^A/d' foo.tex > bar.tex

should work.

  • one can also use an auxiliary tex file to do a read/write obtaining what sed does, but except if you want this also on systems with no sed like line utility, I don't see the point. But it all depends on what is your exact use case, which is not completely clear from your posted question. – user4686 Apr 1 '16 at 13:44
  • I usually set the modified command to display a strikedout old text and, just before submitting, change the command to completely ignore the old text. I wanted a solution that doesn't require me to manually go through the text hunting for these commands. – Tohiko Apr 1 '16 at 14:38
  • 1
    I you follow some simple mark-up rules you can considerably make easier the job later. For example you could use decide to do something like \catcode 1 9 , then \modified{^^A nothing else on same line, ^^A as start of line and put the closing brace } at start of its line. Then you only need to grep for \^^A.* and remove the text matching that patterns. They are many ways, but if you have already used \modified type macros all over the place without any extra mark-up discipline, then it is harder using text editor search/replace. – user4686 Apr 1 '16 at 15:11
  • 0 votes after two years? +1 – Dr. Manuel Kuehner Jun 7 '18 at 16:33
  • 1
    @Dr.ManuelKuehner much appreciated :). I have no idea what this post is all about apart from the fact that I actually do that in my xint package: I use %! to signal private comments which are trimmed when actually building up the CTAN upload... (I used for long time, but using utf-8 make some difficulties with how my active % now act in my private builds, so finally %!). I remove them via sed similar as in my answer here. – user4686 Jun 7 '18 at 16:37

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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