快速制作 26 个宏(每个字母一个)

发布于 2024-09-03 14:26:04 字数 191 浏览 2 评论 0原文

不要为每个字母创建一个宏,如

\def\bA{\mathbf{A}}
...
\def\bZ{\mathbf{Z}}

是否有一种方法可以循环字符类(如大写字母)并为每个字母生成宏?我还想对希腊字母执行相同的操作(使用 bm 而不是 mathbf)。

Instead of making a macro for each letter, as in

\def\bA{\mathbf{A}}
...
\def\bZ{\mathbf{Z}}

Is there a way to loop over a character class (like capital letters) and generate macros for each? I'd also like to do the same for Greek letters (using bm instead of mathbf).

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(4

Saygoodbye 2024-09-10 14:26:04
\def\mydefb#1{\expandafter\def\csname b#1\endcsname{\mathbf{#1}}}
\def\mydefallb#1{\ifx#1\mydefallb\else\mydefb#1\expandafter\mydefallb\fi}
\mydefallb ABCDEFGHIJKLMNOPQRSTUVWXYZ\mydefallb

希腊语新功能

\def\mydefgreek#1{\expandafter\def\csname b#1\endcsname{\text{\boldmath$\mathbf{\csname #1\endcsname}$}}}
\def\mydefallgreek#1{\ifx\mydefallgreek#1\else\mydefgreek{#1}%
   \lowercase{\mydefgreek{#1}}\expandafter\mydefallgreek\fi}
\mydefallgreek {beta}{Gamma}{Delta}{epsilon}{etaex}{Theta}{Iota}{Lambda}{kappa}{mu}{nu}{Xi}{Pi}{rho}\mydefallgreek


$\bGamma\bDelta \bTheta \bLambda \bXi \bPi $

$\bbeta \bgamma\bdelta \bepsilon \betaex \btheta \biota \blambda \bkappa \bmu \bnu \bxi \bpi \brho$
\def\mydefb#1{\expandafter\def\csname b#1\endcsname{\mathbf{#1}}}
\def\mydefallb#1{\ifx#1\mydefallb\else\mydefb#1\expandafter\mydefallb\fi}
\mydefallb ABCDEFGHIJKLMNOPQRSTUVWXYZ\mydefallb

New for Greek

\def\mydefgreek#1{\expandafter\def\csname b#1\endcsname{\text{\boldmath$\mathbf{\csname #1\endcsname}$}}}
\def\mydefallgreek#1{\ifx\mydefallgreek#1\else\mydefgreek{#1}%
   \lowercase{\mydefgreek{#1}}\expandafter\mydefallgreek\fi}
\mydefallgreek {beta}{Gamma}{Delta}{epsilon}{etaex}{Theta}{Iota}{Lambda}{kappa}{mu}{nu}{Xi}{Pi}{rho}\mydefallgreek


$\bGamma\bDelta \bTheta \bLambda \bXi \bPi $

$\bbeta \bgamma\bdelta \bepsilon \betaex \btheta \biota \blambda \bkappa \bmu \bnu \bxi \bpi \brho$
动听の歌 2024-09-10 14:26:04

扩展安德鲁的答案,这是一个没有 \expandafter 的解决方案:

\makeatletter
\@tempcnta=\@ne
\def\@nameedef#1{\expandafter\edef\csname #1\endcsname}
\loop\ifnum\@tempcnta<27
  \@nameedef{b\@Alph\@tempcnta}{\noexpand\mathbb{\@Alph\@tempcnta}}
  \advance\@tempcnta\@ne
\repeat

这将定义 \bA\bB 等,以扩展为\mathbb{A}\mathbb{B} 等等。

Expanding on Andrew's answer, here is a solution without \expandafter:

\makeatletter
\@tempcnta=\@ne
\def\@nameedef#1{\expandafter\edef\csname #1\endcsname}
\loop\ifnum\@tempcnta<27
  \@nameedef{b\@Alph\@tempcnta}{\noexpand\mathbb{\@Alph\@tempcnta}}
  \advance\@tempcnta\@ne
\repeat

This will define \bA, \bB, and so on, to expand to \mathbb{A}, \mathbb{B}, and so on.

明天过后 2024-09-10 14:26:04

定义一个命令不是更好

\newcommand\bm[1]{\ensuremath{${\boldmath$#1$}}$}

,它可以在文本模式和数学模式下使用。
用法:

\[\bm{F(x)}=\int\bm\delta(x)\ dx]
\where \mb F is blah blah blah and \bm \delta is halb halb halb...

结果:
F(x)='inegral delta(x)'dx
其中 F 是 blah blah blah,'delta' 是 halb halb halb...

外部美元用于离开数学(罗马)模式,因为 \boldmath 命令在数学模式下无效。内心的切换回数学(粗体)。附加大括号 (${\boldmath) 确保 \boldmath 命令仅适用于 #1

此代码的另一个优点是测试新命令\bb\bg 的存在。所以你不能轻易地使 LaTeX makros 崩溃。

我希望这就是您正在寻找的。

Wouldn't be better to define one command

\newcommand\bm[1]{\ensuremath{${\boldmath$#1$}}$}

and it can be used both in text mode and math mode.
Usage:

\[\bm{F(x)}=\int\bm\delta(x)\ dx]
\where \mb F is blah blah blah and \bm \delta is halb halb halb...

Result:
F(x)='inegral delta(x)'dx
Where F is blah blah blah and 'delta' is halb halb halb...

Outer dollars are there to leave math (roman) mode because \boldmath command has no effect in math mode. Inner ones switch back to math (bold). Additional braces (${\boldmath) ensures that \boldmath command will work only with #1

Another advantage of this code is testing new commands for existence of \bb and \bg. So you can't crash LaTeX makros easily.

I hope this is what you're looking for.

瑕疵 2024-09-10 14:26:04

我建议这样做:

\newcommand{\b}[1]{\mathbf{#1}}

正如克劳利所说,对于所有其他字母表也是如此。然而,如果你真的想使用 LaTeX 代码来做到这一点,这里有一个似乎可行的方法:

\documentclass{article}
\usepackage{amssymb}
\newcounter{char}
\setcounter{char}{1}

\loop\ifnum\value{char}<27
\edef\c{\Alph{char}}
\expandafter\expandafter\expandafter\expandafter\expandafter\expandafter\expandafter\def\expandafter\expandafter\expandafter\csname\expandafter\expandafter\expandafter b\expandafter\c\expandafter\endcsname\expandafter{\expandafter\mathbb\expandafter{\c}}
\addtocounter{char}{1}
\repeat

\begin{document}

\(\bZ\)
\end{document}

我记不清其中有多少个 'expandafter'!要获得小写字母,请将 Alpha 替换为 alpha。

I would recommend doing:

\newcommand{\b}[1]{\mathbf{#1}}

as Crowley says, and similar for all the other alphabets. However, if you really want to do it using LaTeX code, here's one that seems to work:

\documentclass{article}
\usepackage{amssymb}
\newcounter{char}
\setcounter{char}{1}

\loop\ifnum\value{char}<27
\edef\c{\Alph{char}}
\expandafter\expandafter\expandafter\expandafter\expandafter\expandafter\expandafter\def\expandafter\expandafter\expandafter\csname\expandafter\expandafter\expandafter b\expandafter\c\expandafter\endcsname\expandafter{\expandafter\mathbb\expandafter{\c}}
\addtocounter{char}{1}
\repeat

\begin{document}

\(\bZ\)
\end{document}

I lost count of how many 'expandafter's there are in that! To get lowercase letters, replace the Alph by alph.

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