影响下一个完整单词的 Tex 命令

发布于 2024-08-25 08:27:51 字数 128 浏览 4 评论 0原文

是否有可能有一个 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

天暗了我发光 2024-09-01 08:27:51

首先创建您的命令,例如

 \def\capsimpl#1{{\sc #1}}% Your main macro

捕获空格或标点符号的解决方案:

\catcode`\@=11  
\def\addtopunct#1{\expandafter\let\csname punct@\meaning#1\endcsname\let} 
\addtopunct{ }
\addtopunct{.}    \addtopunct{,}    \addtopunct{?} 
\addtopunct{!}    \addtopunct{;}    \addtopunct{:} 

\newtoks\capsarg
\def\caps{\capsarg{}\futurelet\punctlet\capsx}
\def\capsx{\expandafter\ifx\csname punct@\meaning\punctlet\endcsname\let
       \expandafter\capsend  
       \else \expandafter\continuecaps\fi}

\def\capsend{\expandafter\capsimpl\expandafter{\the\capsarg}}
\def\continuecaps#1{\capsarg=\expandafter{\the\capsarg#1}\futurelet\punctlet\capsx}

\catcode`\@=12

First of all create your command, for example

 \def\capsimpl#1{{\sc #1}}% Your main macro

The solution to catch a space or punctuation:

\catcode`\@=11  
\def\addtopunct#1{\expandafter\let\csname punct@\meaning#1\endcsname\let} 
\addtopunct{ }
\addtopunct{.}    \addtopunct{,}    \addtopunct{?} 
\addtopunct{!}    \addtopunct{;}    \addtopunct{:} 

\newtoks\capsarg
\def\caps{\capsarg{}\futurelet\punctlet\capsx}
\def\capsx{\expandafter\ifx\csname punct@\meaning\punctlet\endcsname\let
       \expandafter\capsend  
       \else \expandafter\continuecaps\fi}

\def\capsend{\expandafter\capsimpl\expandafter{\the\capsarg}}
\def\continuecaps#1{\capsarg=\expandafter{\the\capsarg#1}\futurelet\punctlet\capsx}

\catcode`\@=12
你在我安 2024-09-01 08:27:51

@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!)

绅刃 2024-09-01 08:27:51

关于命令后的空格:请参阅包 xspace 和 TeX FAQ 项目 命令吞噬后面的空格。

现在为什么这非常困难:正如您自己指出的那样,类似的事情似乎只能通过更改目录代码来完成。 Catcodes 在 TeX 读取字符时分配给字符,并且 TeX 一次读取一行,因此您不能对同一行上的其他空格执行任何操作,恕我直言。可能有办法解决这个问题,但我看不到。


下面有危险代码!

此代码将在行尾执行您想要的操作,因此,如果您想要更“流畅”地键入不带括号的内容,但您愿意在每个缩写词后点击“返回”(并且稍后不运行任何自动缩进),您可以使用以下命令:

\def\caps{\begingroup\catcode`^^20 =11\mcaps}
\def\mcaps#1{\def\next##1 {\sc #1##1\catcode`^^20 =10\endgroup\ }\next}

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:

\def\caps{\begingroup\catcode`^^20 =11\mcaps}
\def\mcaps#1{\def\next##1 {\sc #1##1\catcode`^^20 =10\endgroup\ }\next}
时光是把杀猪刀 2024-09-01 08:27:51

一种解决方案可能是将另一个角色设置为活动角色并使用该角色进行转义。这不会消除对结束字符的需要,但会避免键入 \caps 宏,从而使整体更容易键入。

因此,在非常特殊的情况下,进行以下工作。

\catcode`\*=\active
\def*#1*{\textsc{\MakeTextLowercase{#1}}}

Now follows an *Acronym*.

不幸的是,如果没有额外的宏定义,这使得 \section*{} 的使用变得不可能。

在 Xetex 中,似乎可以为此利用 unicode 字符,因此可以定义

\catcode`\•=\active
\def•#1•{\textsc{\MakeTextLowercase{#1}}}

Now follows an •Acronym•.

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.

\catcode`\*=\active
\def*#1*{\textsc{\MakeTextLowercase{#1}}}

Now follows an *Acronym*.

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

\catcode`\•=\active
\def•#1•{\textsc{\MakeTextLowercase{#1}}}

Now follows an •Acronym•.

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文