# [texhax] Extraneous space in macro

Edsko de Vries edsko at edsko.net
Fri Jul 18 13:48:01 CEST 2008

Hi,

I'm trying to write a macro that behaves like \ref but appends the
corresponding page number to a buffer. Everything is working ok, except
that I'm getting an extraneous space. Here is a reduced version that
shows the problem:

\documentclass{article}
\usepackage{patchcmd}

\newcommand{\Buffer}{}
\newcommand{\bref}[1]{%
\patchcommand\Buffer{}{\pageref{#1}}%
\ref{#1}}

\begin{document}

Refer to Section~\bref{sec:first}, Section~\bref{sec:second} and
Section~\bref{sec:third} (\Buffer).

\section{First section}\label{sec:first}
\section{Second section}\label{sec:second}
\section{Third section}\label{sec:third}

\end{document}

If you run the example, you will see that there is too much space in
between the "Section"s and the section numbers. I'm not sure what's
causing the problem; the problem disappears when I remove the
\patchcommand.

Can anyone see what's going wrong? If not, is there an alternative way
to append stuff to a buffer (instead of using \patchcommand)?

Thanks,

Edsko