我想制作一个快捷方式列表,例如myshortcuts={TeX, LaTeX, copyright, mycoommand }
等(mycommand
是用户定义命令的用户定义快捷方式)和一个特殊命令或环境,以便上述每个单词都应自动替换为此特殊环境或命令中的相应命令(例如,,,\TeX
等)。这个问题是“修订”版本的\LaTeX
\copyright
\mycommand
是否可以创建一个环境或命令,使得其中的每个单词都转换为 TeX 命令?
答案1
以下解决方案仅基于 TeX 宏。
\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!{%
\ifx!##2!\addto\tmpb{##1}\else\addto\tmpb{##1#2}\replacestringsA##2!\fi}%
\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\addto\expandafter\setmacrosL\expandafter
{\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:
\setmacros{TeX LaTeX copyright mycommand}
\replacemacros{This is TeX. I don't use LaTeX. Here is an example of mycommand.}
编辑如果您需要将替换的单词放在括号中,{TeX}
那么您必须\detokenize
先替换文本,然后进行替换,然后再次“重新标记”文本:
\def\setmacros#1{\def\setmacrosL{}\setmacrosA#1 {} }
\def\setmacrosA#1 {\ifx^#1^\else
\expandafter\addto\expandafter\setmacrosL\expandafter
{\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}}
原因是这\replacestrings
只是一个宏,它读取分隔的参数。读取参数时,TeX 会隐藏括号中的分隔符。
答案2
尽管我认为完全地不建议,下面的方法或多或少可行,但毫无疑问很容易被破坏。此外,它需要lualatex
。
为了\replaceMacros
成为一个环境,我认为必须使用类别代码(类似于环境Verbatim
)做一些额外的工作。
\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}.
copyright Me (2015).
}
\\[1em]
\setMacros{LaTeX, TeX, copyright, mycommand}
\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}.
copyright Me (2015).
}
\end{document}
答案3
我看不出这种写文档的方式有什么实际用处。不过,只是为了玩玩而已……
\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
\registermacros{TeX,LaTeX,copyright,mycommand}
\newcommand{\mycommand}{`Hello'}
\begin{document}
\replacemacros{This is TeX, in the LaTeX variant,
I can set copyright and say mycommand.}
\end{document}
还有一种环境形式,可以为要替换的附加(本地)命令提供可选参数。
\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
\registermacros{TeX,LaTeX,copyright,mycommand}
\newcommand{\mycommand}{`Hello'}
\begin{document}
\replacemacros{This is TeX, in the LaTeX variant,
I can set copyright and say mycommand.}
\begin{replace}[textbullet]
textbullet This is copyright
\end{replace}
\end{document}
答案4
使用 ConTeXt 的解决方案,我不确定它是否完善(除了可能是我没有正确使用它这一事实)
\usemodule[translate]
\unexpanded\def\examplecommand#1{{\tt#1}}
\translateinput[ConTeXt][\CONTEXT{}]
\translateinput[TeX][\TeX{}]
\translateinput[copyright][©]
\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}.
copyright Me (2015).
\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}.
copyright Me (2015).
\disableinputtranslation
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}.
copyright Me (2015).
\stoptext