# Problems at page boundaries trying to highlight \lstinline

I was trying the code given in Colored background in inline listings, which is actually the same than in How to redefine \lstinline to automatically highlight or draw frames around all inline code snippets?, and I ran into the following problem.

When the paragraph in which the \lstinline appears is broken among pages, the highlight goes to the wrong page (not always). It also happens even if the paragraph is not broken, when latex footnotes or floating mechanism interferes and sends that paragraph to the next page.

The following MWE shows both problems. It is the same code than in the cited answers, with some more text before the example which causes the problem for the first case (page-break at the paragraph). You can uncomment the line with the footnote, near \begin{document}, to see also the second problem (the whole paragraph goes to the next page, but the highlight remains in the first page).

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


This is the result (after compiling twice). The yellow box near "ipsum" should be on the A=1 inline code of the second page.

Can this be fixed?

-

I can offer a solution based on tcolorbox version 2.80 (2014/03/31) released just this day where a \lstinline wrapping is provided with help of xparse. Instead of using the new macro \RenewTotalTCBox from version 2.80 you can also some variation of my answer to How do I create a verbatim box with \newtcbinputlisting? with version 2.72.

To adapt your example, I chose some special character ° to delimit \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}


-