GeneralDefining a Macro which includes a Delimiter

LaTeX specific issues not fitting into one of the other forums of this category.
Post Reply
tafi
Posts: 42
Joined: Sat May 07, 2011 12:01 am

Defining a Macro which includes a Delimiter

Post by tafi »

I am trying to find a shorthand method for typesetting isotopes using XeLaTeX.

The isotope package is old and does not appear to work to well with unicode fonts so I am using the mhchem package instead. Normally to typeset an isotope symbol (lets say Gold-197) you would use this (with mhchem):

\ce{^{197}Au}

My documents include a LOT of different isotopes so i would like to improve speed and readability by using a shorthand macro.
Initially I struck upon the use of this:

\newcommand{\Au}[1]{\ce{^{#1}Au})
\Au{197}

which is more readable and easier to type. It also enabled me to define a symbol for ALL isotopes within one element. However, I hit problems with other symbols (\B for boron, \Re for rhenium and \U for uranium are good examples) becasue they clash with existing commands.

I then tried to define each isotope more explicitly (a lot more work, more command definititons but equally as readable):

\newcommand{\Au-197}{\ce{^{197}Au}\xspace)
\Au-197

But now \newcommand throws a hissy fit. I presume because of the delimiter "-" in the argument.

Instead of \Au-197 I guess I could define a \Au197 command but this will be less readable in the tex file, particularly for single letter elements like U (\U238 is harder to read than \U-238 or \U{238}).

I have thought of using \def instead of \newcommand but I'm pretty sure I can't use latex macros (like \ce) within a lower level tex command like \def. is that right?

Does anyone have a suggestion or recommendation for this?

As I said, readbility and ease/speed of typing are the aim of this and since I will be typing this stuff a lot it is worth takeing a little bit of time finding an elegant solution.

Recommended reading 2024:

LaTeXguide.org • LaTeX-Cookbook.net • TikZ.org

NEW: TikZ book now 40% off at Amazon.com for a short time.

Stefan Kottwitz
Site Admin
Posts: 10348
Joined: Mon Mar 10, 2008 9:44 pm

Re: Defining a Macro which includes a delimiter

Post by Stefan Kottwitz »

Hi,

with LaTeX, you can use only uppercase and lowercase letters in a command name, except commands consisting of a single non-letter symbol.

You may use \def. It's ok to use \def together with LaTeX macros, of course LaTeX macros also often use \def. Just be aware that \def doesn't check if a command is already defined.

Stefan
LaTeX.org admin
User avatar
localghost
Site Moderator
Posts: 9202
Joined: Fri Feb 02, 2007 12:06 pm

Defining a Macro which includes a Delimiter

Post by localghost »

tafi wrote:[…] But now \newcommand throws a hissy fit. I presume because of the delimiter "-" in the argument. […]
Since when has "-" ever been a delimiter? This is simply called a hyphen. Stefan has already given the reasons why your command definition does not work. Alphanumeric names for commands are not allowed.


Thorsten
User avatar
cgnieder
Site Moderator
Posts: 2000
Joined: Sat Apr 16, 2011 7:27 pm

Defining a Macro which includes a Delimiter

Post by cgnieder »

localghost wrote:
tafi wrote:[…] But now \newcommand throws a hissy fit. I presume because of the delimiter "-" in the argument. […]
Since when has "-" ever been a delimiter? This is simply called a hyphen. [...]
Even if the symbol is called a hyphen it's function can be to serve as a delimiter. Like for example the symbol semicolon serves as a delimiter in siunitx' \numlist{2;4;5}. I often us the hyphen as a delimiter in

Code: Select all

\verb-anything-
.
site moderator & package author
tafi
Posts: 42
Joined: Sat May 07, 2011 12:01 am

Defining a Macro which includes a Delimiter

Post by tafi »

Stefan and cgnieder,
Thanks for your help. It is a shame that the command definitions must be so narrow.
localghost wrote: Since when has "-" ever been a delimiter? This is simply called a hyphen.
Thorsten, I am gld that you know what its name is, I had thought that it was a part of the set of delimiters. Perhaps this is wrong, but it is of no consequence to the question I was asking.
localghost wrote: Stefan has already given the reasons why your command definition does not work.
Thorsten
Yes and Stefan has been helpful. I'm still trying to figure out what the meaning of your contribution was...
User avatar
localghost
Site Moderator
Posts: 9202
Joined: Fri Feb 02, 2007 12:06 pm

Defining a Macro which includes a Delimiter

Post by localghost »

cgnieder wrote:[…] Even if the symbol is called a hyphen it's function can be to serve as a delimiter. Like for example the symbol semicolon serves as a delimiter in siunitx' \numlist{2;4;5}. […]
Many other characters can serve as delimiter under special circumstances. But that's no reason to call them as such in general. And here I'd rather consider the semicolons as separators for elements in a list.
cgnieder wrote:[…] I often us the hyphen as a delimiter in

Code: Select all

\verb-anything-
.
Taking your example, many other characters (0…9, =, +, |, ', ", §, …) can be delimiters here. But in general I wouldn't give them this name.
tafi wrote:[…] I am gld that you know what its name is, I had thought that it was a part of the set of delimiters. Perhaps this is wrong, but it is of no consequence to the question I was asking. […]
May be that there are no consequences. But it is misleading in the context of your question.
tafi wrote:[…] I'm still trying to figure out what the meaning of your contribution was.
Our answers just overlapped.
User avatar
cgnieder
Site Moderator
Posts: 2000
Joined: Sat Apr 16, 2011 7:27 pm

Defining a Macro which includes a Delimiter

Post by cgnieder »

localghost wrote:Many other characters can serve as delimiter under special circumstances.
That's what I was trying to say.
localghost wrote:But that's no reason to call them as such in general.
I don't believe, I implied that, did I? It's obvious, there's no reason for that. I also don't think, that tafi implied calling a hyphen "delimiter" in general.
localghost wrote:And here I'd rather consider the semicolons as separators for elements in a list.
I'd prefer that, too. I'd prefer that in the case of the hyphen in

Code: Select all

\U-238
as well, but that doesn't make "delimiter" wrong, does it?
localghost wrote:
cgnieder wrote:[…] I often us the hyphen as a delimiter in

Code: Select all

\verb-anything-
.
Taking your example, many other characters (0…9, =, +, |, ', ", §, …) can be delimiters here.
Yes, I know that. That's the reason, I chose this example, actually.
localghost wrote:But in general I wouldn't give them this name.
As I said before: of course not. But in the case of \verb, I have difficulties to find a better word to describe them.

Back to the topic:

@tafi: I don't know, how much element names clash with existing macros, but if there are only three or for, why not just call them a little different?
For example

Code: Select all

\Bo, \Ur, \RE
for B, U and Re? This is not a perfect solution, but I'm not sure there is one.
site moderator & package author
tafi
Posts: 42
Joined: Sat May 07, 2011 12:01 am

Defining a Macro which includes a Delimiter

Post by tafi »

Hi Cgnieder,
I understand now that I cannot use numbers in the command so I tried another variant:

Code: Select all

\newcommand{\U-}[1]{\ce{^{#1}U}}
so that I can use \U-{238} instead but this doesn't seem to work either. Xelatex still complains that the "command \U is already defined", even though I am trying to define "\U-".

So I cannot use symbols in the command either?

I did use symbols like \Ur or \Bo etc before, but I got complaints from some of those who also occasionally read my source about lack of readability.

If you have an idea by which I can distinguish these element symbols that would be fantastic!
User avatar
cgnieder
Site Moderator
Posts: 2000
Joined: Sat Apr 16, 2011 7:27 pm

Defining a Macro which includes a Delimiter

Post by cgnieder »

Hi Tafi,

Like Stefan said: only letters can be used in the name of a command. So there are to ways:

1) tell LaTeX that "-" is a letter. This is probably a bad idea and would create a real mess, though.
2) maybe define the command depending on a following "-":

Code: Select all

\makeatletter
  \ifdefined\Re
  % save old definition:
  \let\Re@old\Re
  % define new command:
  \renewcommand*\Re{\@ifnextchar-{\Re@aux}{\Re@old}}
  \newcommand*\Re@aux[2]{\ce{^{#2}Re}}
\else
  \newcommand*\Re[2]{\ce{^{#2}Re}}
\fi
\makeatother

\Re-{187}
site moderator & package author
Post Reply