Problemas en los límites de la página al intentar resaltar `\lstinline`

Problemas en los límites de la página al intentar resaltar `\lstinline`

Estaba probando el código dado enFondo de color en listados en línea, que en realidad es lo mismo que en¿Cómo redefinir \lstinline para resaltar o dibujar marcos automáticamente alrededor de todos los fragmentos de código en línea?, y me encontré con el siguiente problema.

Cuando el párrafo en el que \lstinlineaparece está dividido entre páginas, el resaltado va a la página equivocada (no siempre). También sucede incluso si el párrafo no está roto, cuando las notas a pie de página de látex o el mecanismo flotante interfieren y envían ese párrafo a la página siguiente.

El siguiente MWE muestra ambos problemas. Es el mismo código que en las respuestas citadas, con algo más de texto antes del ejemplo que causa el problema en el primer caso (salto de página en el párrafo). Puede descomentar la línea con la nota al pie, cerca de \begin{document}, para ver también el segundo problema (el párrafo completo pasa a la página siguiente, pero lo resaltado permanece en la primera página).

\documentclass[a4paper]{article}
\usepackage{etoolbox}
\usepackage{atbegshi,ifthen,listings,tikz}
\usepackage{lipsum}

% change this to customize the appearance of the highlight
\tikzstyle{highlighter} = [
  yellow,% set the color for inline listings here.
  line width = \baselineskip,
]

% enable these two lines for a more human-looking highlight
%\usetikzlibrary{decorations.pathmorphing}
%\tikzstyle{highlighter} += [decorate, decoration = random steps]

% implementation of the core highlighting logic; do not change!
\newcounter{highlight}[page]
\newcommand{\tikzhighlightanchor}[1]{\ensuremath{\vcenter{\hbox{\tikz[remember picture, overlay]{\coordinate (#1 highlight \arabic{highlight});}}}}}
\newcommand{\bh}[0]{\stepcounter{highlight}\tikzhighlightanchor{begin}}
\newcommand{\eh}[0]{\tikzhighlightanchor{end}}
\AtBeginShipout{\AtBeginShipoutUpperLeft{\ifthenelse{\value{highlight} > 0}{\tikz[remember picture, overlay]{\foreach \stroke in {1,...,\arabic{highlight}} \draw[highlighter] (begin highlight \stroke) -- (end highlight \stroke);}}{}}}
%--------------------------


\makeatletter %   Redefine macros from listings package:
\newtoggle{@InInlineListing}%
\togglefalse{@InInlineListing}%

\renewcommand\lstinline[1][]{%
    \leavevmode\bgroup\toggletrue{@InInlineListing}\bh % \hbox\bgroup --> \bgroup
      \def\lst@boxpos{b}%
      \lsthk@PreSet\lstset{flexiblecolumns,#1}%
      \lsthk@TextStyle
      \@ifnextchar\bgroup{\afterassignment\lst@InlineG \let\@let@token}%
                         \lstinline@}%

\def\lst@LeaveAllModes{%
    \ifnum\lst@mode=\lst@nomode
        \expandafter\lsthk@EndGroup\iftoggle{@InInlineListing}{\eh{}}{}%
    \else
        \expandafter\egroup\expandafter\lst@LeaveAllModes
    \fi%
    }
\makeatother

\lstset{backgroundcolor=\color{green!10}}%

\begin{document}
\lipsum[1-5]
% Foo\footnote{\lipsum[6]}

This is a somewhat large paragraph which latex perhaps will break among two
pages. Hopefully this will show the intended problem, that is, that the
highligthed yellow box is shown in the page in which the paragraph started,
instead of the page in which the code actually landed. Some more sentences to
fill the required space will do. Blah, blah, lorem ipsum or whatever.
This is a test where \lstinline{A=1} should have a yellow background.

This, on the other hand, actually works:
  \begin{lstlisting}[backgroundcolor=\color{green}]
    A = 1
  \end{lstlisting}
\end{document}

Este es el resultado (después de compilar dos veces). El cuadro amarillo cerca de "ipsum" debe estar en el A=1código en línea de la segunda página.

primera páginaSegunda pagina

¿Se puede arreglar esto?

Respuesta1

Puedo ofrecer una solución basada en el tcolorbox version 2.80 (2014/03/31)lanzamiento de este día en el que \lstinlinese proporciona un envoltorio con la ayuda de xparse. En lugar de utilizar la nueva macro \RenewTotalTCBoxde la versión, 2.80también puedes realizar alguna variación de mi respuesta a¿Cómo creo un cuadro textual con \newtcbinputlisting?con versión 2.72.

Para adaptar su ejemplo, elegí algún carácter especial °para delimitar \lstline:

\documentclass[a4paper]{article}
\usepackage{etoolbox}
\usepackage{atbegshi,ifthen,listings,tikz}
\usepackage{lipsum}
\usepackage[most]{tcolorbox}% version 2.80 (2014/03/31)

\let\lstinlineoriginal=\lstinline

% change this to customize the appearance of the highlight
\tikzstyle{highlighter} = [
  yellow,% set the color for inline listings here.
  line width = \baselineskip,
]

\RenewTotalTCBox{\lstinline}{ O{} v }
  {blank,boxsep=1pt,nobeforeafter,tcbox raise base,interior style={fill,highlighter}}
  {\lstinlineoriginal[flexiblecolumns,#1]°#2°}

\lstset{backgroundcolor=\color{green!10}}%

\begin{document}
\lipsum[1-5]
% Foo\footnote{\lipsum[6]}

This is a somewhat large paragraph which latex perhaps will break among two
pages. Hopefully this will show the intended problem, that is, that the
highligthed yellow box is shown in the page in which the paragraph started,
instead of the page in which the code actually landed. Some more sentences to
fill the required space will do. Blah, blah, lorem ipsum or whatever.
This is a test where \lstinline{A=1} should have a yellow background.

This, on the other hand, actually works:
  \begin{lstlisting}[backgroundcolor=\color{green}]
    A = 1
  \end{lstlisting}
\end{document}

ingrese la descripción de la imagen aquí ingrese la descripción de la imagen aquí

información relacionada