Vários hacks espaciais geram mais espaço

Vários hacks espaciais geram mais espaço

Esta é uma continuação de uma postagem recente:Criando comando de comentário removível sem espaço extra. Estou tentando fazer uma coisa comum: criar um comando de comentário removível para que os colaboradores de um documento possam deixar notas uns para os outros e para si próprios, mas para que as notas possam ser removidas automaticamente antes da produção. O problema é que é muito fácil gerar espaço extra aqui.

A solução no post mencionado acabou sendo esta:

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

Funciona muito bem, mas não é perfeito. Especificamente, espaço extra é adicionado no caso de duas notas ocorrerem seguidas. Por exemplo, 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.

Os rendimentos acima:

saída

Como podemos ver, o \@bsphack\@esphackestá eliminando adequadamentealgunsdo espaço redundante e funciona muito bem se sobrar apenas uma nota. Mas se duas notas aparecerem adjacentes uma à outra, mais espaço será gerado no resultado.

Uma abordagem que considerei é que o \notecomando pudesse descobrir que estava próximo a outra nota e evitar emitir o arquivo \@bsphack\@esphack. Porém, minhas tentativas de criar comandos semelhantes no passado foram muito ruins. Alguém já encontrou algo assim? Minhas pesquisas não deram certo.

Obrigado!

Responder1

Um problema é que \@esphackverifica e depois altera o valor do registro \lastskip.

Portanto, \@esphackem instâncias anteriores \noteafeta a maneira como funciona \@esphack em instâncias consecutivas .\note

Uma variante de \@esphackonde ocorre algum "salto para frente e para trás" pode ajudar.

Sinceramente

Ulrich

\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}

informação relacionada