`\lstinline` を強調表示しようとするとページ境界で問題が発生する

`\lstinline` を強調表示しようとするとページ境界で問題が発生する

私は与えられたコードを試していましたインラインリストの背景色、これは実際にはすべてのインライン コード スニペットを自動的に強調表示したり、その周囲にフレームを描画したりするために \lstinline を再定義するにはどうすればよいですか?、そして次のような問題に遭遇しました。

が表示される段落が\lstinlineページ間で分割されている場合、ハイライトは間違ったページに移動します (常にではありません)。段落が分割されていない場合でも、LaTeX の脚注またはフローティング メカニズムが干渉してその段落を次のページに送る場合に発生します。

次の MWE は両方の問題を示しています。これは引用された回答と同じコードですが、最初のケース (段落でページ区切り) の問題の原因となる例の前にテキストが追加されています。 の近くの脚注のある行のコメントを解除すると\begin{document}、2 番目の問題も確認できます (段落全体が次のページに移動しますが、ハイライトは最初のページに残ります)。

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

これは結果です (2 回コンパイルした後)。「ipsum」の近くの黄色のボックスは、A=12 ページ目のインライン コード上にあるはずです。

最初のページ2ページ目

これを修正することはできますか?

答え1

tcolorbox version 2.80 (2014/03/31)私は、この日にリリースされた、\lstinlineの助けを借りてラッピングが提供されるソリューションを提供することができます。バージョンのxparse新しいマクロを使用する代わりに、私の回答のいくつかのバリエーションを使用することもできます。\RenewTotalTCBox2.80\newtcbinputlisting を使用して逐語的ボックスを作成するにはどうすればよいですか?バージョン2.72

°あなたの例を適応させるために、区切りとしていくつかの特殊文字を選択しました\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}

ここに画像の説明を入力してください ここに画像の説明を入力してください

関連情報