Múltiples trucos espaciales generan más espacio

Múltiples trucos espaciales generan más espacio

Esta es una continuación de una publicación reciente:Crear comando de comentario extraíble sin espacio adicional. Estoy tratando de hacer algo común: crear un comando de comentario extraíble para que los contribuyentes a un documento puedan dejar notas entre sí y para ellos mismos, pero para que las notas se puedan eliminar automáticamente antes de la producción. El problema es que es muy fácil generar espacio extra aquí.

La solución en la publicación antes mencionada resultó ser esta:

\newif\ifnotes
\makeatletter
\newcommand{\note}[1]{\ifnotes{#1}\else\@bsphack\@esphack\fi}
\makeatother

Funciona bastante bien, pero no es perfecto. En concreto, se añade espacio extra en el caso de que aparezcan dos notas seguidas. Por ejemplo, considere:

\notesfalse

Testing \note{X} testing testing.

Testing \note{X}\note{Y} testing testing.

Testing \note{X} \note{Y} testing testing.

\notestrue

Testing \note{X} testing testing.

Testing \note{X}\note{Y} testing testing.

Testing \note{X} \note{Y} testing testing.

Lo anterior produce:

producción

Como podemos ver, se \@bsphack\@esphackestá eliminando adecuadamente.algunodel espacio redundante y funciona bastante bien si sólo queda una nota. Pero si dos notas aparecen una al lado de la otra, se genera más espacio en el resultado.

Un enfoque que he considerado es que el \notecomando podría descubrir que estaba al lado de otra nota y luego evitar emitir el extra \@bsphack\@esphack. Sin embargo, mis intentos de crear comandos similares en el pasado han salido bastante mal. ¿Alguien ha encontrado algo como esto? Mis búsquedas resultaron vacías.

¡Gracias!

Respuesta1

Un problema es que \@esphackverifica y luego cambia el valor del registro \lastskip.

Por lo tanto, lo \@esphackocurrido en instancias anteriores \noteafecta la forma en que funciona \@esphack en instancias consecutivas .\note

Podría ser útil una variante de \@esphackdónde se producen algunos "saltos hacia adelante y hacia atrás".

Atentamente

Ulrico

\documentclass{article}

\newif\ifnotes
\makeatletter
\newcommand{\note}[1]{%
  \@bsphack
%=== instead of \@esphack: ===
%  \showthe\lastskip  
  \relax
  \ifhmode
    \spacefactor\@savsf
    \ifdim\@savsk>\z@
      \nobreak
      \hskip\z@skip
% The previous action will change \lastskip, so:
      \hskip-\@savsk
      \hskip\@savsk      
% now \lastskip is almost \@savsk again.
%      \showthe\lastskip
      \ignorespaces
    \fi
  \fi
%===========================
  \ifnotes #1\fi
}
\makeatother

\begin{document}

\notesfalse

Testing\note{X}testing testing.

Testing \note{X}testing testing.

Testing\note{X} testing testing.

Testing \note{X} testing testing.

Testing\note{X}\note{Y}testing testing.

Testing\note{X}\note{Y} testing testing.

Testing\note{X} \note{Y}testing testing.

Testing\note{X} \note{Y} testing testing.

Testing \note{X} \note{Y}testing testing.

Testing \note{X} \note{Y} testing testing.

Testing \note{X}\note{Y} testing testing.

Testing \note{X}\note{Y}testing testing.

\notestrue

Testing\note{X}testing testing.

Testing \note{X}testing testing.

Testing\note{X} testing testing.

Testing \note{X} testing testing.

Testing\note{X}\note{Y}testing testing.

Testing\note{X}\note{Y} testing testing.

Testing\note{X} \note{Y}testing testing.

Testing\note{X} \note{Y} testing testing.

Testing \note{X} \note{Y}testing testing.

Testing \note{X} \note{Y} testing testing.

Testing \note{X}\note{Y} testing testing.

Testing \note{X}\note{Y}testing testing.

\end{document}

información relacionada