Foreach loop in Preamble
Similar to Skillmon, but using etoolbox
for simpler code. I also recommend prefixing the macros to avoid unintentionally overwriting build in macros.
This can easily be adapted to also make others.
\documentclass{article}
\usepackage{etoolbox,bm}
\newcommand\Maker[1]{
\ifcsundef{bf#1}{
\csdef{bf#1}{\bm{#1}}
}{
\typeout{Cannot make \string\bf#1, already exists, ignoring}
}
}
\forcsvlist\Maker{a,b,c,d,e,f,g,h,i,j,k,l,m,n,o,p,q,r,s,t,u,v,w,x,y,z}
\forcsvlist\Maker{A,B,C,D,E,F,G,H,I,J,K,L,M,N,O,P,Q,R,S,T,U,V,W,X,Y,Z}
\begin{document}
\[
\bfa
\]
\end{document}
The following is a way to use a for loop in LaTeX which doesn't require any packages:
\documentclass[]{article}
\makeatletter
\@for\cs:={S,V,E,M,A,z,x,B,I,J,X,Q}\do{
\expandafter\newcommand\csname my\cs\endcsname{}% check whether the command is already defined
\expandafter\edef\csname my\cs\endcsname{\noexpand\mathbf{\cs}}% define it expanding \cs
}
\makeatother
\begin{document}
$\myS$
\end{document}
If you want clean syntax, expl3
is for you.
\usepackage{expl3}
\ExplSyntaxOn % access the programmer's level
\clist_map_inline:nn
{ S, V, E, M, A, z, x, B, I, J, X, Q }
{
\cs_new_protected:cpn {b#1} { \mathbf{#1} }
}
\ExplSyntaxOff
This will define \bS
, \bV
and so on. Avoid redefining commands such as \S
. If you want to be on the risky side, use \cs_set_protected:cpn
instead of \cs_new_protected:cpn
, but then blame yourself if something goes wrong.