我有这样一个文件:
This \word{is} some text.
This is some \word{more text}.
\word{This} is \word{yet} some more \word{text}.
我需要创建出现在 \word{
和匹配的右括号 }
之间的所有文本的列表,例如:
is
more text
This
yet
text
- 左大括号和右大括号始终出现在同一行,绝不会跨行。
- 其他大括号出现在文档中,但没有出现在
\word{}
中。
如何打印 \word{}
中出现的所有文本的列表?
最佳答案
看来您正在处理一个 TeX 文件...那么为什么不直接使用 TeX 来执行此操作呢?然后你会确定不会有任何问题和副作用,例如,
\word {there's a space between \verb=\word= and the curly bracket}
这仍然有效!它仍然适用于多行内容:
\word{this is
a multiline stuff \emph{and you can even add more groupings in it,}
it'll still work fine!}
在您的 (La)TeX 序言中,只需添加:
\newwrite\file
\immediate\openout\file=output.txt
\def\word#1{\immediate\write\file{#1}}
如果您使用的是 LaTeX 而不是 plainTeX,则使用 \newcommand
。
您还可以将 \immediate\write\file{#1}
放在您的 \word
定义宏中。如果您无权访问 \word
宏(例如,它在类或样式文件中),您可以:
\let\oldword\word
\def\word#1{\immediate\write\file{#1}\oldword{#1}}
希望这对您有所帮助!
关于bash - 如何在 BASH 中找到所有出现在 `\word{}` 之间的单词?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17509911/