在LaTeX中用字符串替换字符 [英] Replace a character with a string in LaTeX

查看:1407
本文介绍了在LaTeX中用字符串替换字符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在寻找一种方法来对LaTeX中的字符串进行子字符串替换.我想做的是构建一个我可以这样调用的命令:

I'm looking for a way to do a substring replace on a string in LaTeX. What I'd like to do is build a command that I can call like this:

\replace{File,New}

这会生成类似

\textbf{File}$\rightarrow$\textbf{New}

这是一个简单的示例,但是我希望能够将格式设置/结构放在单个命令中,而不是文档中的所有位置.我知道我可以构建几个使用越来越多参数的命令,但是我希望有一种更简单的方法.

This is a simple example, but I'd like to be able to put formatting/structure in a single command rather than everywhere in the document. I know that I could build several commands that take increasing numbers of parameters, but I'm hoping that there is an easier way.

编辑以澄清

我正在寻找

string.replace(",", "$\rightarrow$)

可以接受任意字符串,然后用另一个子字符串替换子字符串的东西.

something that can take an arbitrary string, and replace a substring with another substring.

因此,我可以使用\ replace {File},\ replace {File,New},\ replace {File,Options,User}等调用该命令,以粗体格式包装单词,并用替换所有逗号右箭头命令.即使用粗体包裹"位太困难(如我认为的那样),仅替换部分也会有所帮助.

So I could call the command with \replace{File}, \replace{File,New}, \replace{File,Options,User}, etc., wrap the words with bold formatting, and replace any commas with the right arrow command. Even if the "wrapping with bold" bit is too difficult (as I think it might be), just the replace part would be helpful.

推荐答案

一般情况比较棘手(当您不使用逗号作为分隔符时),但是您给出的示例可以在编写代码时没有太多麻烦LaTeX内部知识.

The general case is rather more tricky (when you're not using commas as separators), but the example you gave can be coded without too much trouble with some knowledge of the LaTeX internals.

\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中用字符串替换字符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆