General ⇒ Defining a Macro which includes a Delimiter
Defining a Macro which includes a Delimiter
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.
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.
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
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
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
- localghost
- Site Moderator
- Posts: 9202
- Joined: Fri Feb 02, 2007 12:06 pm
Defining a Macro which includes a Delimiter
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.tafi wrote:[…] But now \newcommand throws a hissy fit. I presume because of the delimiter "-" in the argument. […]
Thorsten
How to make a "Minimal Example"
Board Rules
Avoidable Mistakes
¹ System: TeX Live 2025 (vanilla), TeXworks 0.6.10
Board Rules
Avoidable Mistakes
¹ System: TeX Live 2025 (vanilla), TeXworks 0.6.10
Defining a Macro which includes a Delimiter
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 inlocalghost wrote:Since when has "-" ever been a delimiter? This is simply called a hyphen. [...]tafi wrote:[…] But now \newcommand throws a hissy fit. I presume because of the delimiter "-" in the argument. […]
Code: Select all
\verb-anything-
site moderator & package author
Defining a Macro which includes a Delimiter
Stefan and cgnieder,
Thanks for your help. It is a shame that the command definitions must be so narrow.
Thanks for your help. It is a shame that the command definitions must be so narrow.
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: Since when has "-" ever been a delimiter? This is simply called a hyphen.
Yes and Stefan has been helpful. I'm still trying to figure out what the meaning of your contribution was...localghost wrote: Stefan has already given the reasons why your command definition does not work.
Thorsten
- localghost
- Site Moderator
- Posts: 9202
- Joined: Fri Feb 02, 2007 12:06 pm
Defining a Macro which includes a Delimiter
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.
Taking your example, many other characters (0…9, =, +, |, ', ", §, …) can be delimiters here. But in general I wouldn't give them this name.cgnieder wrote:[…] I often us the hyphen as a delimiter in.Code: Select all
\verb-anything-
May be that there are no consequences. But it is misleading in the context of your question.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. […]
Our answers just overlapped.tafi wrote:[…] I'm still trying to figure out what the meaning of your contribution was.
How to make a "Minimal Example"
Board Rules
Avoidable Mistakes
¹ System: TeX Live 2025 (vanilla), TeXworks 0.6.10
Board Rules
Avoidable Mistakes
¹ System: TeX Live 2025 (vanilla), TeXworks 0.6.10
Defining a Macro which includes a Delimiter
That's what I was trying to say.localghost wrote:Many other characters can serve as delimiter under special circumstances.
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:But that's no reason to call them as such in general.
I'd prefer that, too. I'd prefer that in the case of the hyphen inlocalghost wrote:And here I'd rather consider the semicolons as separators for elements in a list.
Code: Select all
\U-238
Yes, I know that. That's the reason, I chose this example, actually.localghost wrote:Taking your example, many other characters (0…9, =, +, |, ', ", §, …) can be delimiters here.cgnieder wrote:[…] I often us the hyphen as a delimiter in.Code: Select all
\verb-anything-
As I said before: of course not. But in the case of \verb, I have difficulties to find a better word to describe them.localghost wrote:But in general I wouldn't give them this name.
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
site moderator & package author
Defining a Macro which includes a Delimiter
Hi Cgnieder,
I understand now that I cannot use numbers in the command so I tried another variant:
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!
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 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!
Defining a Macro which includes a Delimiter
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 "-":
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