3

Following this previous question, I have in my acronyms the same pattern I define in a \newcommand\BS.

However, when I use \ensuremath and type my acronym with no math environment, I get an obvious extra space before and after, whereas it's alright if I put it in math mode.

enter image description here

It has to do with When not to use \ensuremath for math macro but even if I follow the advice to put extra { } inside \ensuremath{} to surround my expression, I still get extra spacing.

NB :If you tell me "well put your acronym in math mode then", i'd reply "well I have to do that along my 1500 pages, so how can I avoid that ? :) " and get the proper spacing in text ?

\documentclass[12pt,a4paper]{article}
\usepackage[utf8]{inputenc}
\usepackage{amsmath}
\usepackage{amsfonts}
\usepackage{amssymb}
\usepackage{acro}
%\usepackage{xspace}

% with extra {} inside \ensuremath{ } 
\newcommand{\BS}[2]{ \ensuremath{ { \mathcal{#1}_{\mathcal{#2} } } } }

\DeclareAcronym{ki}{
short= \BS{K}{I},
long = Knock-In
}
\begin{document}

When I use \acs{ki} in text there is obviously extra space before and after my acronym.

When I use $\acs{ki}$ in math there is the right space before and after my acronym.

\printacronyms

\end{document}
4
  • 1
    why do you add all these spaces in the definition? Feb 11, 2022 at 18:08
  • @UlrikeFischer well it was for readibility of my code and thought we did not care about spaces but it seems that we do ! Sorry for this silly question Ulrike :/
    – JeT
    Feb 11, 2022 at 18:10
  • 1
    well one difference between math and text is that spaces matters differently in text. Feb 11, 2022 at 18:11
  • @UlrikeFischer It's obvious once you say it :) Thank and sorry for the silly question. Shall we close it ?
    – JeT
    Feb 11, 2022 at 18:14

1 Answer 1

6

You're responsible of those spaces. I denote by ! the significant ones and with x those that are ignored because they are processed in math mode:

\newcommand{\BS}[2]{ \ensuremath{ { \mathcal{#1}_{\mathcal{#2} } } } }
                    !            x x                          x x x !

Don't use \ensuremath in the definition of \BS: you gain nothing in being able to type some \BS{K}{I} text instead of the clearer and more semantic some $\BS{K}{I}$ text. There are also useless braces that are better removed.

\documentclass[12pt,a4paper]{article}
\usepackage{acro}

\newcommand{\BS}[2]{\mathcal{#1}_{\mathcal{#2}}}

\DeclareAcronym{ki}{
  short= \ensuremath{\BS{K}{I}},
  long = Knock-In
}

\begin{document}

When I use \acs{ki} in text

When I use $\acs{ki}$ in math

\printacronyms

\end{document}

enter image description here

1
  • Guilty :) I must confess when I see code with expl3 the extra spaces give more readibility in the code. Wrong choice of mine to generalize.
    – JeT
    Feb 11, 2022 at 18:48

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .