
Eu estava tentando o código fornecido emFundo colorido em listagens inline, que na verdade é o mesmo que emComo redefinir \lstinline para destacar ou desenhar quadros automaticamente em torno de todos os trechos de código embutidos?, e me deparei com o seguinte problema.
Quando o parágrafo em que \lstinline
aparece está quebrado entre páginas, o destaque vai para a página errada (nem sempre). Também acontece mesmo que o parágrafo não esteja quebrado, quando notas de rodapé de látex ou mecanismo flutuante interferem e enviam aquele parágrafo para a próxima página.
O seguinte MWE mostra ambos os problemas. É o mesmo código das respostas citadas, com mais algum texto antes do exemplo que causa o problema para o primeiro caso (quebra de página no parágrafo). Você pode descomentar a linha com a nota de rodapé, perto de \begin{document}
, para ver também o segundo problema (o parágrafo inteiro vai para a próxima página, mas o destaque permanece na primeira 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 é o resultado (depois de compilar duas vezes). A caixa amarela perto de “ipsum” deve estar no A=1
código embutido da segunda página.
Isso pode ser corrigido?
Responder1
Posso oferecer uma solução baseada no tcolorbox
version 2.80 (2014/03/31)
lançamento hoje em que um \lstinline
embrulho é fornecido com a ajuda do xparse
. Em vez de usar a nova macro \RenewTotalTCBox
da versão, 2.80
você também pode fazer alguma variação da minha resposta paraComo faço para criar uma caixa literal com \newtcbinputlisting?com versão 2.72
.
Para adaptar seu exemplo, escolhi alguns caracteres especiais °
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}