1

I am currently using many simple substitutions like:

\newcommand{\abc}{{\small ABC}}
\newcommand{\ABC}{{\abc} }

And then in my document text:

I use \ABC if I want a space and if not then \abc.

I'm using this pattern for small caps because the output from \textsc is either too big (for uppercase) or too small (for lowercase), but I use it for accents too.

However, for me it's much better if I can just use one command like so:

I use \abc if I want a space and if not then I can still use \abc.

So, my questions:

  1. Is there a way to combine the \ABC and \abc commands into just \abc by intelligently detecting punctuation? I guess this would require asking the parser for a lookahead token.

  2. If so, can the interface be something easy like:

\smartnewcommand{\abc}{\small ABC}
  1. And can the interface also handle:
\smartnewcommand{\def}{d\'{e}f}
cjfp
  • 13
  • 1
    You should give package xspace a try for this. – Miyase Jul 14 '22 at 23:26
  • 1
    I will try it, thanks. However, I just found this discussion where the author recommends against using it. Instead it seems like using \abc{} for a space is better practice. https://tex.stackexchange.com/questions/86565/drawbacks-of-xspace – cjfp Jul 14 '22 at 23:33
  • 4
    yes xspace does what you ask but I wouldn't use it – David Carlisle Jul 15 '22 at 00:00
  • Alright, I can probably just pre-process with awk '{print gensub(/(\\[[:alpha:]]+) /, "\\1{} ", "g")}' foo.tex. If that causes problems maybe I can generate a specific list of commands from my .tex source for awk to recognize – cjfp Jul 15 '22 at 01:10

2 Answers2

1

You're asking the wrong question.

Since you have, as you say, many simple substitutions such as “abc → ABC (in smaller font)”, it's much better to mark them up rather than going to

\newcommand{\abc}{{\small ABC}\xspace}

(see Adding {} at the end of a command vs xspace) or, better,

\newcommand{\abc}{{\small ABC}}

and getting used to type \abc{}.

What do I mean by “marking up”? Your substitutions are uniform, so you can do

\documentclass{article}

\ExplSyntaxOn \NewDocumentCommand{\sm}{m} {{ \small \text_uppercase:n { #1 } }} \ExplSyntaxOff

\begin{document}

I use \sm{abc} if I want a space and if not then \sm{abc}.

I can even use \sm{ABC}.

\end{document}

and not worry about spaces at all.

This has several benefits: one is that you don't have to define many commands and it's already a huge benefit. But the main asset here is that you can change the appearance of each of these snippets at once by just modifying the definition.

Say that you realize that your substitutions happen to be in footnotes as well: now hardwiring \small in them is really wrong, isn't it? What about using 90% of the current font size? Or a fraction that you may want to change once you decide the document font.

\RequirePackage{fix-cm} % or use a different scalable document font

\documentclass{article}

\ExplSyntaxOn \NewDocumentCommand{\sm}{m} {{ \cjfp_reduce_font:n { 0.9 } \text_uppercase:n { #1 } }}

\cs_new_protected:Nn \cjfp_reduce_font:n { \fontsize{ \fp_eval:n { #1 * \use:c { f@size } } } { 0 } \selectfont } \ExplSyntaxOff

\begin{document}

I use \sm{abc} if I want a space and if not then \sm{abc}.

I can even use \sm{ABC}.

{\footnotesize Here is \sm{abc} in a footnote}

\end{document}

enter image description here

egreg
  • 1,121,712
  • This is an excellent answer to "How do I write a customizable \textsc?" I agree that it's cleaner than creating a new macro for each acronym. What I really want is some kind of autotexify so that I can write more plain text, but I feel like this needs a pre-processor. That said, I'm accepting the other answer since it's more direct and general. – cjfp Jul 21 '22 at 17:49
0

Try using \abc{} everywhere, even before punctuations.

\documentclass[]{article}

\newcommand{\abc}{ABC}

\begin{document} \abc{} in text, before comma \abc{}, and before fullstop \abc{}. \end{document}

Output:

enter image description here

Zxcvasdf
  • 1,735