# Is it possible to automatically replace commands shortcuts with the corresponding TeX commands?

I would like to make a list of shortcuts like myshortcuts={TeX, LaTeX, copyright, mycoommand } etc. (mycommand is a user-defined shortcut of a user-defined command) and a special command or enviroment so that each of the above words should be autommatically replaced by its coresponding command inside this special enviroment or command ( like \TeX, \LaTeX, \copyright , \mycommand etc.) This question is a "revised" version of Is it possible to make an environment or a command such that every word inside it is transformed into a TeX command?

• Why can't you use the methods there? – cfr Dec 18 '15 at 5:21
• It depends on your environment editor. Which text editor do you use ? Or, If you use linux, you are able to modify your tex file with commands such as sed. – Jérôme Dequeker Dec 18 '15 at 6:33
• With any sensible text editor this is not a problem at all. Vim's LaTeX-suite does exactly that for a basic set of commands. – Bananguin Dec 18 '15 at 8:52
• under xelatex you can use package xesearch – user4686 Dec 18 '15 at 12:13
• @jfbu Thank you for the package. It looks interesting. I have not linux, Vim only windows without power editors, In any case, thank you all for your interesting! – kornaros Dec 19 '15 at 21:01

The following solution is based simply on TeX macros.

\long\def\addto#1#2{\expandafter\def\expandafter#1\expandafter{#1#2}}
\bgroup \catcode!=3 \catcode?=3  % replacestrings from opmac.tex
\gdef\replacestrings#1#2{\long\def\replacestringsA##1#1##2!{%
\edef\tmpb{\expandafter}\expandafter\replacestringsA\tmpb?#1!%
\long\def\replacestringsA##1?{\def\tmpb{##1}}\expandafter\replacestringsA\tmpb
}
\egroup

\def\setmacros#1{\def\setmacrosL{}\setmacrosA#1 {} }
\def\setmacrosA#1 {\ifx^#1^\else
{\expandafter\replacestringsX\csname#1\endcsname{#1}}%
\expandafter\setmacrosA \fi
}
\def\replacestringsX#1#2{\replacestrings{#2}{#1}}
\def\replacemacros#1{\def\tmpb{#1}\setmacrosL\tmpb}

\def\mycommand{Nazdar}

% test:

\replacemacros{This is TeX. I don't use LaTeX. Here is an example of mycommand.}


Edit If you need to put replaced words in braces like {TeX} then you must \detokenize the replaced text first, then do replacement and then "retokenize" the text again:

\def\setmacros#1{\def\setmacrosL{}\setmacrosA#1 {} }
\def\setmacrosA#1 {\ifx^#1^\else
{\expandafter\replacestringsX\csname#1\expandafter\endcsname
\expandafter{\detokenize{#1}}}%
\expandafter\setmacrosA \fi
}
\def\replacestringsX#1#2{\replacestrings{#2}{#1}}
\def\replacemacros#1{\edef\tmpb{\detokenize{#1}}\setmacrosL
\scantokens\expandafter{\tmpb}}


The reason is that the \replacestrings is only macro and it reads separated parameters. TeX hides separators in braces when the parameter is read.

• I have only one problem. I can not use { } like {TeX} In this way I can not use commands inside other commands I dont understand the reason! – kornaros Dec 19 '15 at 21:33
• @kornaros OK, see "edit" part of my answer. – wipet Dec 20 '15 at 7:28

Although I think it is completely inadvisable, the following more or less works, but can no doubt be broken very easily. Also, it requires lualatex.

For \replaceMacros to be an environment, I think some extra work would have to be done with category codes (similar to the Verbatim environment).

\documentclass[varwidth,border=5]{standalone}
\usepackage{luacode}
\begin{luacode*}
replaceMacros = function (input, macros)
local s, x, y
local list = {}
string.gsub(macros, '(%a+)',
function(x) table.insert(list, x) end)
input = ' ' .. input .. ':'
for _, s in ipairs(list) do
input = string.gsub(input, '%s('.. s .. ')([^%a])',
function(x, y)
if x and y then
if y == ' ' then  y = '\\space ' end
return ' \\' .. x  .. y
end
end)
end
return input:sub(2, input:len()-1)
end

\end{luacode*}

\def\setMacros#1{\def\currentMacros{#1}}
\setMacros{}
\long\def\replaceMacros#1{%
\directlua{tex.print(%
replaceMacros('\luatexluaescapestring{\detokenize{#1}}',
'\currentMacros'))}%
}

\def\mycommand#1{\texttt{#1}}

\begin{document}
\replaceMacros{This is LaTeX which is built on top of TeX.
Here is an example of mycommand{typewriter text}.
And \textbf{Look!} \emph{Other macros still work}.
}
\\[1em]
\replaceMacros{This is LaTeX which is built on top of TeX.
Here is an example of mycommand{typewriter text}.
And \textbf{Look!} \emph{Other macros still work}.
}
\end{document}


• Since you're using the luacode package, you could use its \luastring macro and write \luastring{\detokenize{#1}} instead of '\luatexluaescapestring{\detokenize{#1}}'. – Mico Dec 20 '15 at 9:13

I can't see any real usefulness of such a way of writing documents. But, just for playing around…

\documentclass{article}
\usepackage{xparse,l3regex}

\ExplSyntaxOn
\NewDocumentCommand{\registermacros}{m}
{
\clist_map_inline:nn { #1 }
{
\seq_put_right:Nn \l_kornaros_replace_macros_seq { ##1 }
}
\tl_set:Nx \l_kornaros_replace_macros_tl
{
(\seq_use:Nn \l_kornaros_replace_macros_seq { | })
}
}

\NewDocumentCommand{\replacemacros}{m}
{
\tl_set:Nn \l_kornaros_replace_text_tl { #1 }
\regex_replace_all:VnN \l_kornaros_replace_macros_tl { \c{\1} } \l_kornaros_replace_text_tl
\tl_use:N \l_kornaros_replace_text_tl
}

\seq_new:N \l_kornaros_replace_macros_seq
\tl_new:N \l_kornaros_replace_macros_tl
\tl_new:N \l_kornaros_replace_text_tl
\cs_generate_variant:Nn \regex_replace_all:nnN { V }
\ExplSyntaxOff

\newcommand{\mycommand}{Hello'}

\begin{document}

\replacemacros{This is TeX, in the LaTeX variant,
I can set copyright and say mycommand.}

\end{document}


With also an environment form, that can be given an optional argument for additional (local) commands to be replaced.

\documentclass{article}
\usepackage{xparse,l3regex,environ}

\ExplSyntaxOn
% User interface
\NewDocumentCommand{\registermacros}{m}
{
\kornaros_replace_register:n { #1 }
}
\NewDocumentCommand{\replacemacros}{m}
{
\kornaros_replace_main:n { #1 }
}

\NewEnviron{replace}[1][]
{
\kornaros_replace_register:n { #1 }
\kornaros_replace_main:V \BODY
}

\cs_new_protected:Nn  \kornaros_replace_register:n
{
\clist_map_inline:nn { #1 }
{
\seq_put_right:Nn \l_kornaros_replace_macros_seq { ##1 }
}
\tl_set:Nx \l_kornaros_replace_macros_tl
{
(\seq_use:Nn \l_kornaros_replace_macros_seq { | })
}
}
\cs_new_protected:Nn \kornaros_replace_main:n
{
\tl_set:Nn \l_kornaros_replace_text_tl { #1 }
\regex_replace_all:VnN \l_kornaros_replace_macros_tl { \c{\1} } \l_kornaros_replace_text_tl
\tl_use:N \l_kornaros_replace_text_tl
}
\cs_generate_variant:Nn \kornaros_replace_main:n { V }

\seq_new:N \l_kornaros_replace_macros_seq
\tl_new:N \l_kornaros_replace_macros_tl
\tl_new:N \l_kornaros_replace_text_tl
\cs_generate_variant:Nn \regex_replace_all:nnN { V }
\ExplSyntaxOff

\newcommand{\mycommand}{Hello'}

\begin{document}

\replacemacros{This is TeX, in the LaTeX variant,
I can set copyright and say mycommand.}

\begin{replace}[textbullet]
\end{replace}

\end{document}


• kornaros inside a command! I don't believe it!!! – kornaros Dec 22 '15 at 5:24

A solution with ConTeXt, I'm not sure if it's polished (apart from the fact that may be I'm not using it correctly)

\usemodule[translate]

\unexpanded\def\examplecommand#1{{\tt#1}}

\translateinput[ConTeXt][\CONTEXT{}]
\translateinput[TeX][\TeX{}]
\translateinput[examplecommand][\examplecommand]

\starttext

This is ConTeXt which is built on top of TeX.
Here is an example of examplecommand{typewriter text}.
And \bold{Look!} \emph{Other macros still work}.

\enableinputtranslation

This is ConTeXt which is built on top of TeX.
Here is an example of examplecommand{typewriter text}.
And \bold{Look!} \emph{Other macros still work}.