我正在寻找一种在LaTeX中对字符串进行子字符串替换的方法。我想做的是构建一个我可以这样调用的命令:
\replace{File,New}
那会产生类似
\textbf{File}$\rightarrow$\textbf{New}
这是一个简单的示例,但是我希望能够将格式设置/结构放在单个命令中,而不是文档中的所有位置。我知道我可以构建几个使用越来越多参数的命令,但是我希望有一种更简单的方法。
编辑以澄清
我正在寻找一个等价的
string.replace(",", "$\rightarrow$)
可以采用任意字符串,然后用另一个子字符串替换一个子字符串的东西。
因此,我可以使用\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}
关于latex - 在LaTeX中用字符串替换字符,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/95824/