GeneralHow to amalgamate commands?

LaTeX specific issues not fitting into one of the other forums of this category.
gaius
Posts: 5
Joined: Wed Jan 09, 2008 2:46 pm

How to amalgamate commands?

Post by gaius »

Hi all,
let's say one defines a simple command \boldletter{X} which prints the bold version of its argument (X in this case).

How can one define a shorthand for this command, e.g. \bletX = \boldletter{X} such that it would work for all letters in the alphabet A...Z and a...z - so for instance: \bletG = \boldletter{G}.

Of course, this is easy to do by defining new commands \bletA, \bletB, ... one by one.
But can this
\blet + "*" = \boldletter{"*"}
be done more elegantly for all "*"?

Thanks in advance for your help.

Recommended reading 2024:

LaTeXguide.org • LaTeX-Cookbook.net • TikZ.org
LaTeX Beginner's Guide LaTeX Cookbook LaTeX TikZ graphics TikZによるLaTeXグラフィックス
balfonsi
Posts: 93
Joined: Wed Mar 14, 2007 12:05 am

Re: How to amalgamate commands?

Post by balfonsi »

If it's only for single characters you just have to type \textbf A (without brackets}, \textbf B and so on.

B.A.
gaius
Posts: 5
Joined: Wed Jan 09, 2008 2:46 pm

Re: How to amalgamate commands?

Post by gaius »

thanks for your reply.

That's true for all commands, and that's the way I've been doing it until now.

The reason for wanting to actually join them is that I find it easier (e.g. for debugging) to read the code by having composite commands.
curiouslearn
Posts: 105
Joined: Fri Nov 30, 2007 11:32 pm

Re: How to amalgamate commands?

Post by curiouslearn »

I am quite new to latex and do not know the exact solution to your question. However, I know how you can define a shorthand \blet{X}=\boldletter{X} which will work for all alphabets. You can try,

\newcommand{\blet}[1]{\boldletter{#1}}

Once you define the above command you can use \blet{X} to get \boldletter{X} where X is any alphabet.
gaius
Posts: 5
Joined: Wed Jan 09, 2008 2:46 pm

Re: How to amalgamate commands?

Post by gaius »

Thanks for your reply.
I know that, of course.
The only reason for having chosen the bold example was to serve as an illustration of the problem.

To make things clearer: I do know how to write text and math in boldface, as well as defining/redefining commands.

As explained in the first post, what I'm really looking for is a way to create composite commands in general, whereby \fooXX is interpreted as \foo{XX} for whatever XX. Do I need to use TeX primitives for that, or can it be done in LaTeX?
User avatar
localghost
Site Moderator
Posts: 9201
Joined: Fri Feb 02, 2007 12:06 pm

How to amalgamate commands?

Post by localghost »

gaius wrote: […] As explained in the first post, what I'm really looking for is a way to create composite commands in general, whereby \fooXX is interpreted as \foo{XX} for whatever XX. Do I need to use TeX primitives for that, or can it be done in LaTeX?

In which way would that make typesetting easier? Or isn't that the intention of your question?


Best regards
Thorsten
How to make a "Minimal Example"
Board Rules
Avoidable Mistakes[/size]

¹ System: openSUSE 42.2 (Linux 4.4.52), TeX Live 2016 (vanilla), TeXworks 0.6.1
gaius
Posts: 5
Joined: Wed Jan 09, 2008 2:46 pm

How to amalgamate commands?

Post by gaius »

In which way would that make typesetting easier? Or isn't that the intention of your question?


This question came up when I was trying to define an upright version for all symbols in the basic Math Letters font (I collected and patched together a set of roman shaped symbols from different fonts, covering the Math letters set).

Instead of having a style file with lots of definitions like
\DeclareMathSymbol{\alphaup}{\mathord}{lettersup}{11}
one for each upright symbol \xxxxxup.

It would be nice if one could make it so that the "up" suffix (or prefix) would make the rest of the command Roman shaped.
At the time I ended up doing it symbol by symbol as exemplified above, but am still curious if/how it can be done in LaTeX.
It's not so much that it would be extremely useful or facilitate typesetting terribly, but the solution would probably be instructive for other situations.

regards
curiouslearn
Posts: 105
Joined: Fri Nov 30, 2007 11:32 pm

Re: How to amalgamate commands?

Post by curiouslearn »

Even I would love to learn if there is an easy way for what Gaius is asking. It may make typing much more convenient and easier in some instances.
User avatar
Juanjo
Posts: 657
Joined: Sat Jan 27, 2007 12:46 am

How to amalgamate commands?

Post by Juanjo »

The following code does not solve the original problem, but I think it may illustrate the difficulties one may find:

Code: Select all

\documentclass[]{article}

\makeatletter
\newcounter{ii} \setcounter{ii}{1}
\@whilenum{\value{ii}<27}\do{%
    \def\temp{\Alph{ii}}
    \expandafter\edef\csname foo\temp\endcsname{This is letter~\fbox{\temp}}
    \stepcounter{ii}}
\makeatother

\begin{document}
\fooA. \fooB. \fooC\ldots\fooZ. \par
\bfseries \fooA. \fooB. \fooC\ldots\fooZ.
\end{document}
Here one defines commands of the form \fooX, where X is A, B, C, D and so on. The command \fooX writes the sentence "This is letter X", where X is inside a frame. The code needs TeX commands as \edef or \expandafter. Let us observe that \fooX is equivalent to \foo{X}, with

Code: Select all

\newcommand{\foo}[1]{This is letter~\fbox{#1}}
In the above code, I've tried something like "\textbf{\temp}" instead of "This is letter~\fbox{\temp}". But then the compilation fails due to reasons that I ignore. It seems that one can't directly put in the definition commands or declarations that change the font attributes. Surely, there are better ways to get these commands and remove these limitations, of course, with the help of a true TeX hacker. Anyway, it is by far much more quick to write the 27 definitions of \fooA,...,\fooZ than develop the above piece of code!

Edited to add this:

In the preamble, one can replace the \makeatletter--\makeatother block by the following lines:

Code: Select all

\makeatletter
 \@for\temp:={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}\do{%
 \expandafter\edef\csname foo\temp\endcsname{This is letter~\fbox{\temp}}}
\makeatother
As seen here, the loop that provides the \fooX commands can be done differently.
gaius
Posts: 5
Joined: Wed Jan 09, 2008 2:46 pm

How to amalgamate commands?

Post by gaius »

I had also tried something like you (using \expandafter after reading Appendix C of Kopka & Daly) to delay evaluation/expansion, but was less successful than you.

In the meantime I found two packages where they seem to do exactly this: one is txfonts (package for Times fonts) and the other is wrisym (package for Mathematica files).

In the case of wrisym they have commands like \dsX defined as \mathbb{X}, and similarly for the Fraktur and \mathcal alphabets. In the case \mathbb they first define \dsX individually for each character, and later redefine \mathbb{X} to act as \dsX for general alphanumeric X.
The definition of \mathbb is

Code: Select all

\DeclareRobustCommand*{\mathbb}[1]{%
  \gdef\F@ntPrefix{ds}
  \@EachCharacter #1\@EndEachCharacter
 }
which seems to depend only on the following definitions earlier in the package

Code: Select all

\long\def\DoLongFutureLet #1#2#3#4{%
    \def\@FutureLetDecide{%
        #1#2\@FutureLetToken
            \def\@FutureLetNext{#3}%
        \else
            \def\@FutureLetNext{#4}%
        \fi
        \@FutureLetNext
    }%
\futurelet\@FutureLetToken\@FutureLetDecide}
\def\DoFutureLet #1#2#3#4{\DoLongFutureLet{#1}{#2}{#3}{#4}}

\def\EachCharacter #1\EndEachCharackter{%
  \@EachCharacter #1\@EndEachCharacter
}

\def\@EachCharacter{%
  \DoFutureLet{\ifx}{\@EndEachCharacter}%
    {\@EachCharacterDone}%
	{\@PickUpTheCharacter}%
}
And in the case of txfonts they do exactly the same for \varmathbb - an alternative version of \mathbb

Code: Select all

\long\def\DoLongFutureLet #1#2#3#4{%
   \def\@FutureLetDecide{#1#2\@FutureLetToken
      \def\@FutureLetNext{#3}\else
      \def\@FutureLetNext{#4}\fi\@FutureLetNext}
   \futurelet\@FutureLetToken\@FutureLetDecide}
\def\DoFutureLet #1#2#3#4{\DoLongFutureLet{#1}{#2}{#3}{#4}}
\def\@EachCharacter{\DoFutureLet{\ifx}{\@EndEachCharacter}%
   {\@EachCharacterDone}{\@PickUpTheCharacter}}
\def\m@keCharacter#1{\csname\F@ntPrefix#1\endcsname}
\def\@PickUpTheCharacter#1{\m@keCharacter{#1}\@EachCharacter}
\def\@EachCharacterDone \@EndEachCharacter{}

\DeclareRobustCommand*{\varmathbb}[1]{\gdef\F@ntPrefix{m@thbbch@r}%
  \@EachCharacter #1\@EndEachCharacter}
It isn't easy to understand without knowing a bit more about TeX, so I'm off to get me one in the near future. In the meantime, does anyone understand this completely?
Post Reply