4

How would I define a macro \test , e.g. using \def\test#1{...}, that can be used in the two following ways? The usage without a blank

\test{A}This is some text

as well as the usage with a blank

\test{A} This is some text

should yield the same typeset result:

A: This is some text

This would help me in making life easier while writing my free physics text.

  • 1
    \def\test#1{#1: \ignorespaces} – Manuel Jun 19 '16 at 10:25
8

The code

\newcommand\test[1]{#1:~\ignorespaces}

will do what you want. The space should be part of the replacement text, as you seem to want it; with \ignorespace, spaces following the argument will be gobbled.

6

Package xspace can be your friend:

\documentclass{article}
\usepackage{xspace}
\newcommand*{\test}[1]{#1:\xspace}
\begin{document}
\test{A}This is some text

\test{A} This is some text
\end{document}

Result

However, xspace does not add a space before punctuation characters, for example, its usage is intended for stuff inside text. Manuels's solution or egreg's answer are probably better in this case, because it always sets one space and ignores additional spaces.

  • Hm, I already use 55 packages, adding one more is dangerous. I'd prefer an explicit code... – Motion Mountain Jun 19 '16 at 10:25
  • @MotionMountain “adding one more is dangerous” Why? – clemens Jun 19 '16 at 10:27
  • 3
    @MotionMountain The grade of danger heavily depends on the packages. There are packages, small, quite independent, which post not much of a danger, there are packages, quite interfering with LaTeX internals or conflict with other packages. Package xspace belongs rather to the first category. – Heiko Oberdiek Jun 19 '16 at 10:39
  • OK, I'll test it and let you know. – Motion Mountain Jun 19 '16 at 10:41
  • xspace loads without problems, but I need \ignorespaces nevertheless, as my code already adds a space. – Motion Mountain Jun 19 '16 at 13:35

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.