Short: Is there a way to prevent \xspace from inserting white space if it is at the beginning of a line?
Long: I have defined several formatting macros for use in normal text. One of these (\indoubt) is normally used inside of text to insert annotations. In the normal context, there should be just one space between the preceding word and the output of this macro, so I used \unskip\xspace at the beginning of this macro. However, I also want to display a list of all formatting macros that are actually used in the document -- using the same macro to output some sample text. (I could manually write this text without \xspace, but that is error prone because I would have to remember to change the example text if I ever change the formatting in the macro.) In this list, \xspace is right at the beginning of the line. Is there a way to get rid of it in this place?
Here is an MWE:
\documentclass{article}
\usepackage{xspace}
\usepackage{xifthen}
\usepackage{etoolbox}
\makeatletter
\newcommand\addmacro[1]{%
\write\@auxout{\noexpand\@writefile{macros}{#1\newline}}%
}
\newcommand\printmacros{%
\section*{Meaning of formats}%
\@starttoc{macros}%
}
\makeatother
% Once a formatting macro has been used, set the corresponding flag to TRUE
% and write self-formatted explanation to legend
\newcommand{\checkflag}[2]{%
\providebool{flag#1}%
\ifbool{flag#1}
{}
{\global\booltrue{flag#1}%
\addmacro{#2}%
}%
}
% Express doubt if unsure about a word in the text
\newcommand{\indoubt}[1]{%
\checkflag{indoubt}{\protect\indoubt{Macro with leading xspace}}%
\ifthenelse{\equal{#1}{}}
{\unskip{\textbf{\xspace[?]\xspace}}}
{\unskip{\textbf{\xspace[#1]\xspace}}}%
}
\newcommand{\simple}[1]{%
\checkflag{simple}{\simple{Just a plain formatting directive.}}%
\textsc{#1\xspace}%
}
\setlength{\parindent}{0pt}
\begin{document}
\printmacros
\section{Test}
This is some textt\indoubt{Wrong spelling?}~\dots \simple{More text.}
\end{document}
\xspace[? – egreg Oct 18 '17 at 17:16\xspace. My opinion: Don't use\xspaceespecially not in this context. Preceding a macro with\xspacejust seems wrong to me. Just leave a space between the macro (\indoubt) and the preceding word. – Skillmon Oct 18 '17 at 17:19\xspace! I just want to annotate some words in the text, like this: "This is a sonett [looks German -- in English it's spelled 'sonnet'.] by Shakespeare.", with braces marking the insertions as not belonging to the original. – Andreas Oct 18 '17 at 17:23\xspace[just makes TeX spin its wheels for doing nothing useful. – egreg Oct 18 '17 at 17:25"This is a sonett \indoubt{looks German} by Shakespeare"and remove thexspacefrom your code. – Skillmon Oct 18 '17 at 17:25\xspacedoes anything useful in your code. Just remove it. And remove the useless braces (although not really harmful). – egreg Oct 18 '17 at 17:27\colorto highlight the inserted text. In order to keep that color change to just that insertion I inserted the text in extra braces. So you're right, they are quite pointless in this example. Got to leave now, though, so I'll get back to the other comments later on. :-) – Andreas Oct 18 '17 at 17:33\unskip~would have the same effect because in my macro definition I already know what character will be the next one ([). However, I opted for\xspacebecause its ability to insert a space only on certain conditions appealed to me -- and because I believed it could somehow be convinced to behave in a special way at the start of a line. – Andreas Oct 19 '17 at 10:59word\indoubt{}and I later on decide to delete this word, I will be less likely to forget to also remove the annotation. In this way, the macro\indoubt{}would be similar to\footnote{}. – Andreas Oct 19 '17 at 11:15\xspace[just makes TeX spin its wheels for doing nothing useful": Is that because of the unnecessary call of\xspace(which could be replaced by a simple~), or is it the combination with[that causes the "spinning wheels"? In the latter case, I guess that would be because[could be interpreted as an optional argument. But then, wouldn't it also be a problem to write[after any command if you just want to output the character[? – Andreas Oct 19 '17 at 11:34\xspacewas meant to be at the trailing position in a parameterless macro. It's completely useless elsewhere (or even harmful). – egreg Oct 19 '17 at 11:58\xspaceat the end of a macro definition and just a space or\space(which are synonymous by default) at the beginning or in the middle of a macro definition to insert a space unless it would be at the start of a line. – Andreas Oct 19 '17 at 12:53