我正在寻找一种方法在LaTeX中对字符串进行子串替换.我想做的是构建一个我可以这样调用的命令:
\replace{File,New}
Run Code Online (Sandbox Code Playgroud)
这会产生类似的东西
\textbf{File}$\rightarrow$\textbf{New}
Run Code Online (Sandbox Code Playgroud)
这是一个简单的例子,但我希望能够将格式/结构放在单个命令中而不是文档中的任何位置.我知道我可以构建几个占用越来越多参数的命令,但我希望有一种更简单的方法.
编辑以澄清
我正在寻找相当于
string.replace(",", "$\rightarrow$)
Run Code Online (Sandbox Code Playgroud)
可以采用任意字符串的东西,并用另一个子字符串替换子字符串.
所以我可以使用\ replace {File},\ replace {File,New},\ replace {File,Options,User}等来调用命令,用粗体格式包装单词,并用右箭头命令替换任何逗号.即使"粗体包裹"位太难(我认为可能),只需更换部件就会有所帮助.
一般情况下比较棘手(当你不使用逗号作为分隔符时),但你给出的例子可以编码而不会对LaTeX内部的一些知识造成太多麻烦.
\documentclass[12pt]{article}
\makeatletter
\newcommand\formatnice[1]{%
\let\@formatsep\@formatsepinit
\@for\@ii:=#1\do{%
\@formatsep
\formatentry{\@ii}%
}%
}
\def\@formatsepinit{\let\@formatsep\formatsep}
\makeatother
\newcommand\formatsep{,}
\newcommand\formatentry[1]{#1}
\begin{document}
\formatnice{abc,def}
\renewcommand\formatsep{\,$\rightarrow$\,}
\renewcommand\formatentry[1]{\textbf{#1}}
\formatnice{abc,def}
\end{document}
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
18432 次 |
最近记录: |