影响下一个完整单词的 Tex 命令
是否有可能有一个 TeX 命令将整个下一个单词(或下一个字母,但不包括下一个标点符号)作为参数,而不仅仅是下一个字母或 {} 组?
我想对某些首字母缩略词使用 \caps 命令,但不想一遍又一遍地键入大括号。
Is it possible to have a TeX command which will take the whole next word (or the next letters up to but not including the next punctuation symbol) as an argument and not only the next letter or {} group?
I’d like to have a \caps command on certain acronyms but don’t want to type curly brackets over and over.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
首先创建您的命令,例如
捕获空格或标点符号的解决方案:
First of all create your command, for example
The solution to catch a space or punctuation:
@Debilski - 我在我的论文中为首字母缩写词编写了类似于您的活动 * 代码的内容。我激活了<然后 \def<#1>打印首字母缩略词,以及扩展(如果是第一次遇到)。我还通过允许内联定义扩展并使用 .aux 文件将扩展发送“回到过去”(如果它们在声明之前使用),或者如果是首字母缩略词则报告错误,这也有点离题了。从未被声明过。
总的来说,这在当时似乎是一个好主意 - 我很少需要 <<在我的实际文本中为 catcode 12(因为我的所有宏都在单独的 .sty 文件中),并且我使其在数学模式下运行,因此我无法预见任何困难。但天哪,它很脆弱……我不知道有多少次我因为改变一些看似无关的东西而意外地破坏了我的构建。综上所述,激活即使是远程常用的字符也要非常小心。
另一方面,使用 XeTeX 和更高的 unicode 字符,它可能更安全,并且通常有简单的方法来键入这些额外的字符,例如制作一个多(或组合)键(我通常映射 numlock 或其中一个) windows 键),以便例如多!-!产生¡)。或者,如果您在 emacs 中运行,则可以使用 C-\ 短暂切换到 TeX 输入模式,通过键入 TeX 命令来插入 unicode(尽管这对于实际键入 TeX 文档来说很痛苦,因为它会拦截您实际的 \的,请不要尝试定义自己的转义字符!)
@Debilski - I wrote something similar to your active * code for the acronyms in my thesis. I activated < and then \def<#1> to print the acronym, as well as the expansion if it's the first time it's encountered. I also went a bit off the deep end by allowing defining the expansions in-line and using the .aux files to send the expansions "back in time" if they're used before they're declared, or to report errors if an acronym is never declared.
Overall, it seemed like it would be a good idea at the time - I rarely needed < to be catcode 12 in my actual text (since all my macros were in a separate .sty file), and I made it behave in math mode, so I couldn't foresee any difficulties. But boy was it brittle... I don't know how many times I accidentally broke my build by changing something seemingly unrelated. So all that to say, be very careful activating characters that are even remotely commonly-used.
On the other hand, with XeTeX and higher unicode characters, it's probably a lot safer, and there are generally easy ways to type these extra characters, such as making a multi (or compose) key (I usually map either numlock or one of the windows keys to this), so that e.g. multi-!-! produces ¡). Or if you're running in emacs, you can use C-\ to switch into TeX input mode briefly to insert unicode by typing the TeX command for it (though this is a pain for actually typing TeX documents, since it intercepts your actual \'s, and please please don't try defining your own escape character!)
关于命令后的空格:请参阅包
xspace
和 TeX FAQ 项目 命令吞噬后面的空格。现在为什么这非常困难:正如您自己指出的那样,类似的事情似乎只能通过更改目录代码来完成。 Catcodes 在 TeX 读取字符时分配给字符,并且 TeX 一次读取一行,因此您不能对同一行上的其他空格执行任何操作,恕我直言。可能有办法解决这个问题,但我看不到。
下面有危险代码!
此代码将仅在行尾执行您想要的操作,因此,如果您想要更“流畅”地键入不带括号的内容,但您愿意在每个缩写词后点击“返回”(并且稍后不运行任何自动缩进),您可以使用以下命令:
Regarding whitespace after commands: see package
xspace
, and TeX FAQ item Commands gobble following space.Now why this is very difficult: as you noted yourself, things like that can only be done by changing catcodes, it seems. Catcodes are assigned to characters when TeX reads them, and TeX reads one line at a time, so you can not do anything with other spaces on the same line, IMHO. There might be a way around this, but I do not see it.
Dangerous code below!
This code will do what you want only at the end of the line, so if what you want is more "fluent" typing without brackets, but you are willing to hit 'return' after each acronym (and not run any auto-indent later), you can use this:
一种解决方案可能是将另一个角色设置为活动角色并使用该角色进行转义。这不会消除对结束字符的需要,但会避免键入
\caps
宏,从而使整体更容易键入。因此,在非常特殊的情况下,进行以下工作。
不幸的是,如果没有额外的宏定义,这使得
\section*{}
的使用变得不可能。在 Xetex 中,似乎可以为此利用 unicode 字符,因此可以定义
Which 应该减少对其他命令的影响,但当然需要将字符“•”映射到键盘上某个地方才能使用。
One solution might be setting another character as active and using this one for escaping. This does not remove the need for a closing character but avoids typing the
\caps
macro, thus making it overall easier to type.Therefore under very special circumstances, the following works.
Unfortunately, this makes uses of
\section*{}
impossible without additional macro definitions.In Xetex, it seems to be possible to exploit unicode characters for this, so one could define
Which should reduce the effects on other commands but of course needs to have the character ‘•’ mapped to the keyboard somewhere to be of use.