5

I would like to add \xspace to the Latin phrases of the chemmacros package because I don't want to use \ or {} at the end of commands without arguments.
However, when I try to define a new Latin phrase like \NewChemLatin{\testtest}{testtest\xspace}, the \xspace is ignored.
Also, I would like to avoid having to renew all the already defined Latin phrases or to add new helper commands like \newcommand{\xinsitu}{\insitu\xspace} or something like that.

MWE:

\documentclass{article}
\usepackage{chemmacros,xspace}
\NewChemLatin{\test}{test\xspace}
\newcommand{\xtest}{\test\xspace}
\begin{document}
    This is a \test within a sentence.
    And this is an end-of-sentence \test.

    Now, this is an x-\xtest within a sentence.
    And this is an end-of-sentence x-\xtest.
\end{document}

So, how can this be achieved?

7

When you do \NewChemLatin{\test}{test\xspace} and call \test, the result is, essentially,

\emph{test\xspace}

so \xspace actually sees } and doesn't add the space.

You might do

\NewChemLatin{\innertest}{test}
\newcommand{\test}{\innertest\xspace}

Full example:

\documentclass{article}
\usepackage{chemmacros,xspace}

\NewChemLatin{\innertest}{test}
\newcommand{\test}{\innertest\xspace}

\begin{document}

This is a \test within a sentence.
And this is an end-of-sentence \test.

\end{document}

enter image description here

  1. Don't use \xspace. Here is why: Drawbacks of xspace
  2. Don't use minimal. Here is why: Why should the minimal class be avoided?
  • So, there is no way to avoid that helper command (\innertest) and I will have to write one for all the already existing phrases I guess? That last part just read like a slap on my hands, so I edited the answer for future unenlightened folks that stumble upon this answer. – basseur Aug 23 '18 at 13:37
  • @basseur chemmacros used to have support for xspace, but it was dropped with version 5. – egreg Aug 23 '18 at 14:14
0

I was able to solve the problem of having to redefine each Latin phrase one by one or having to use helper commands as in egregs answer.

Because

  1. the \NewChemLatin{constant}{phrase} command actually pretty much just creates a constant with the value phrase which is wrapped in whatever is defined for the nomenclature modules format option (standard is \emph)

and

  1. all predefined Latin phrases (\insitu, \invacuo and \abinitio) are only later defined in an \AtBeginDocument hook (see Renewing Latin Phrases in chemmacros does not work)

the \NewChemLatincommand can simply be overwritten via \RenewDocumentCommand to fit your needs. This way you can add \xspace or whatever you need. The only drawback (if it is any) I can think of is, that you will not be able to use the format option of the nomenclature module.

\documentclass{article}
\usepackage{chemmacros}
\usepackage{xspace}

%to overwrite the \NewChemLatin command which is used for all Latin phrases
\RenewDocumentCommand{\NewChemLatin}{mm}{
    \newcommand*{#1}{\emph{#2}\xspace}
}

%new constants will be formatted the same ways a s the standard ones
\NewChemLatin{\test}{test}

\begin{document}
    Mid sentence \insitu   and at the end: \insitu.   \\
    Mid sentence \invacuo  and at the end: \invacuo.  \\
    Mid sentence \abinitio and at the end: \abinitio. \\
    Mid sentence \test     and at the end: \test.     \\
\end{document}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.