What you can easily do to a certain degree with (La)TeX is copying a source text file to a target text file in a way where
(La)TeX does under verbatim-category-code-régime read the source file line by line and write the target file line by line.
Hereby you can have (La)TeX perform some replacements within each line before writing it to the target file.
With the approach outlined by me replacement is done only within lines but not across linebreaks.
With the approach outlined by me replacement-copying does not take place across several \input-files.
But you can probably use it for writing a little script in LaTeX for processing/replace-copying all your input files.
I decided to implement this by "hacking" the verbatimcopy package (Version 2008/11/17 v0.06) by Lars Madsen and me.
That "hack" yielded another small package, called verbatimreplacementcopy.sty :
% verbatimreplacementcopy.sty (C) 2016 by Ulrich Diez.
% Licence: LPPL.
\ProvidesPackage{verbatimreplacementcopy}[2016/12/08 v0.02 beta by Ulrich Diez]
\RequirePackage{verbatimcopy}[2008/11/17]
\begingroup
\newcommand*\VerbatimCopyB[2]{% {from file}{to file}
\@bsphack
\expandafter\def\expandafter\VC@target\expandafter{\VC@outputdir#2}%
\IfFileExists{\VC@@quote#1\VC@@quote}%
{%
\bgroup
\def\@verbatim{%
\obeylines
\let\do\@makeother
\dospecials
}%
\let\endtrivlist\relax
\def\verbatim@processline{%
\begingroup
\edef\verbatim@line{\the\verbatim@line}%
\@onelevel@sanitize{\verbatim@line}%
\verbatim@replacementhook
\expandafter\endgroup
\expandafter\verbatim@line\expandafter{\verbatim@line}%
\immediate\write\verbatim@out{\the\verbatim@line}%
}%
\immediate\openout\verbatim@out\VC@@quote\VC@target\VC@@quote\relax
\verbatiminput{\VC@@quote#1\VC@@quote}%
\immediate\closeout\verbatim@out%
\egroup%
}%
{%
\PackageError{verbatimcopy}%
{Source-file cannot be found}%
{%
For copying source-file to target-file it would be nice to
have the source-file available.%
}%
}%
\@esphack
}%
\ifx\VerbatimCopy\OldVerbatimCopy
\expandafter\global\expandafter\let\csname VerbatimCopy\endcsname\VerbatimCopyB
\fi
\global\let\OldVerbatimCopy\VerbatimCopyB
\endgroup
\newcommand\verbatim@replacementhook{}%
\newcounter{verbatim@replacements}%
\newcommand\Replace{}%
\outer\def\Replace{%
\stepcounter{verbatim@replacements}%
\VCverbaction{\VCverbaction{\expandafter\VC@repldef\@firstofone}}{}%
}%
\newcommand\Noreplacements{}%
\outer\def\Noreplacements{\def\verbatim@replacementhook{}}%
\newcommand*\VC@repldef[2]{%
\expandafter\VC@@repldef
\csname VC@@repl\number\value{verbatim@replacements}\expandafter\endcsname
\csname VC@repl\number\value{verbatim@replacements}\endcsname{#1}{#2}%
}%
\newcommand*\VC@@repldef[4]{%
\@ifdefinable#1{%
\def#1##1#3##2\@nil{%
\ifx\@nil##2\@nil
\expandafter\@firstoftwo
\else
\expandafter\@secondoftwo
\fi
{##1}{##1#4#1##2\@nil}%
}%
}%
\newcommand*#2[1]{#1##1#3\@nil}%
\expandafter\def\expandafter\verbatim@replacementhook\expandafter{%
\verbatim@replacementhook
\edef\verbatim@line{\expandafter#2\expandafter{\verbatim@line}}%
}%
}%
\endinput
As verbatimreplacementcopy.sty is based on the verbatimcopy package, I strongly recommend reading the manual of that package. The manual explains how to change the target directory etc. It also points out that the \VerbatimCopy-macro will overwrite and thus destroy the target file without warnings!
You can call verbatimreplacementcopy.sty from within a script for copy-replacing text files. Let's call such a script Replacer.tex:
\documentclass{minimal}
\usepackage{verbatimreplacementcopy}
\Replace{My Unique Chapter Name}{ch:my-unique-chapter-name}
\Replace{My other Unique Chapter Name}{ch:my-other-unique-chapter-name}
\VerbatimCopy{Original.tex}{CopyA.tex}%
\Noreplacements
\Replace{My Unique Chapter Name}{ch:my/unique/chapter/name}
\Replace{My other Unique Chapter Name}{ch:my/other/unique/chapter/name}
\VerbatimCopy{Original.tex}{CopyB.tex}%
\stop
\Replace will add another replacement directive for \VerbatimCopy.
Replacement directives will be applied successively by \VerbatimCopy whenever \VerbatimCopy is copying a text file.
\Noreplacements will clear all replacement directives so that no replacement directive will be applied by \VerbatimCopy when \VerbatimCopy is copying a text file.
Therefore this script will now copy the file Original.tex to the file CopyA.tex.
Hereby with each line of Original.tex the following will be done:
- Each phrase "My Unique Chapter Name" will be replaced by "ch:my-unique-chapter-name".
- In the result of that replacement each phrase "My other Unique Chapter Name" will be replaced by "ch:my-other-unique-chapter-name".
Then the \Noreplacements-directive does clear all replacement directives.
Then this script will copy the file Original.tex to the file CopyB.tex.
Hereby with each line of Original.tex the following will be done:
- Each phrase "My Unique Chapter Name" will be replaced by "ch:my/unique/chapter/name".
- In the result of that replacement each phrase "My other Unique Chapter Name" will be replaced by "ch:my/other/unique/chapter/name".
If the content of Original.tex was
My Unique Chapter Name. Some text. My other Unique Chapter Name Some
text text. My other Unique Chapter Name. Phrases. Text.
Chapter Name. My Unique Chapter Name text text phrase
, the content of CopyA.tex will be:
ch:my-unique-chapter-name. Some text. ch:my-other-unique-chapter-name Some
text text. ch:my-other-unique-chapter-name. Phrases. Text.
Chapter Name. ch:my-unique-chapter-name text text phrase
and the content of CopyB.tex will be:
ch:my/unique/chapter/name. Some text. ch:my/other/unique/chapter/name Some
text text. ch:my/other/unique/chapter/name. Phrases. Text.
Chapter Name. ch:my/unique/chapter/name text text phrase
U s a g e i s a t y o u r o w n r i s k !
I give no warranties.
If something breaks I am not interested in the pieces.
(I am interested in bug reports.)
\hyperef{ch:my-unique-chapter-name}{My Unique Chapter Name}or even just\ref{ch:my-unique-chapter-name}– David Carlisle Dec 05 '16 at 16:59\newcommand{\MyChap}{\hyperef{ch:my-unique-chapter-name}{My Unique Chapter Name}}and use it instead of the plain text version – Bordaigorl Dec 05 '16 at 17:11\hyperrefs. However, LuaLaTeX intercepts this and does a string replacement, replacing the matched unique chapter name X with\hyperref[link]{X}. Is this what you're after? – Werner Dec 06 '16 at 06:18sedorawk. Someone once used TeX to control Mars Rover, which makes for a fascinating read. But I'm not recommending it as a general solution.... – jon Dec 07 '16 at 03:34