我想从命令\lstinline
中排版逐字样式的命令\Block
,但它似乎不起作用
\documentclass[a4paper,12pt]{article}
\usepackage{nicematrix}
\usepackage{listings}
\lstdefinestyle{verbatim}{basicstyle=\ttfamily,language={[LaTeX]TeX}}
\begin{document}
\lstinline[style=verbatim]|\mathcal|
\begin{NiceTabularX}{\textwidth}[]{*{11}X[c, t]}
\Block{1-}{\lstinline[style=verbatim]|\mathcal|} & & & & & & & & & &
\\
\end{NiceTabularX}
\end{document}
编辑 从以下答案中木子木志Z和埃格尔,似乎有两种解决方案可能,如下所示
解决方案 1
\newsavebox{\lstinlineBox}
然后将参数放入lrbox
环境中,如下所示
\begin{lrbox}{\lstinlineBox}\lstinline[style=verbatim]|\mathcal|\end{lrbox}
然后我们可以按如下方式发布箱子
\usebox{\lstinlineBox}
该方法的优点
- 它适用于
\lstinline
帮助listings
根据许多编程语言的预定义样式处理代码 - 你在参数中输入任何内容
\lstinline
,它都会按照排版方式显示(即不需要转义规则)。该参数会根据你的编辑器偏好突出显示
该方法的缺点
- 字体大小被修改,并且不会从周围文本继承
解决方案 2
使用\EscVerb{\\mathcal}
来自fvextra
包
该方法的优点
- 比
listings
- 字体大小继承自周围的文本
该方法的缺点
您需要满足
fvextra
包文档中提供的转义规则(v1.4 的第 14 页)。例如,要排版 '',您必须输入\\
没有突出显示的论点
\EscVerb
,这意味着修改和阅读更加困难
我认为,最好能提供一种解决方案,以巧妙的方式克服这种限制。
\documentclass[a4paper,12pt]{article}
\usepackage{nicematrix}
\usepackage{listings}
\lstdefinestyle{verbatim}{basicstyle=\ttfamily,language={[LaTeX]TeX}}
\newsavebox{\lstinlineBox}
\usepackage{fvextra}
\begin{document}
\begin{lrbox}{\lstinlineBox}\lstinline[style=verbatim]|\mathcal|\end{lrbox}
\section{\usebox{\lstinlineBox}}
\noindent
\begin{NiceTabularX}{\textwidth}[]{*{11}X[c, t]}
\begin{lrbox}{\lstinlineBox}\lstinline[style=verbatim]|\mathcal|\end{lrbox}
\Block{1-}{\usebox{\lstinlineBox}} \\
0 & 1 & 2 & 3 & 4 & 5 & 6 & 7 & 8 & 9 & \lstinline[style=verbatim]|\mathcal|
\end{NiceTabularX}
\bigskip
\begin{lrbox}{\lstinlineBox}\lstinline[style=verbatim]|\mathcal|\end{lrbox}
\section{\EscVerb{\\mathcal}}
\noindent
\begin{NiceTabularX}{\textwidth}[]{*{11}X[c, t]}
\Block{1-}{\EscVerb{\\mathcal}} \\
0 & 1 & 2 & 3 & 4 & 5 & 6 & 7 & 8 & 9 & \EscVerb{\\mathcal}
\end{NiceTabularX}
\end{document}
答案1
您可以定义一个Blockenv
环境:
\documentclass[a4paper,12pt]{article}
\usepackage{nicematrix}
\usepackage{listings}
\lstdefinestyle{verbatim}{basicstyle=\ttfamily,language={[LaTeX]TeX}}
\newsavebox{\Blockenvbox}
\NewDocumentEnvironment{Blockenv}{O{}m}
{\begin{lrbox}{\Blockenvbox}}
{\end{lrbox}\Block[#1]{#2}{\usebox\Blockenvbox}}
\begin{document}
\noindent
\begin{NiceTabularX}{\textwidth}[]{*{11}X[c, t]}
\begin{Blockenv}{1-}
\lstinline[style=verbatim]|\mathcal|
\end{Blockenv} \\
0 & 1 & 2 & 3 & 4 & 5 & 6 & 7 & 8 & 9 & 10
\end{NiceTabularX}
\bigskip
\noindent
\begin{NiceTabularX}{\textwidth}[]{*{11}X[c, t]}
\begin{Blockenv}[l]{1-}
\lstinline[style=verbatim]|\mathcal|
\end{Blockenv} \\
0 & 1 & 2 & 3 & 4 & 5 & 6 & 7 & 8 & 9 & 10
\end{NiceTabularX}
\end{document}
警告。当\lstinline
在 内部发出时Blockenv
,会出现一条Overfull \hbox
可能来自测量阶段的虚假消息,但似乎不会影响最终结果。
答案2
如果你对其他逐字包持开放态度,并且更喜欢就地输入(相比David Carlisle 的建议),然后你可以使用
fvextra
只有逐字 + 换行,minted
基于fvextra
并通过 python 库提供代码高亮pygments
(-shell-escape
必需)。
包裹cprotect
可能是另一种选择但我没有这样做。
\EscVerb
显示和 的示例\EscMintinline
:
\documentclass[a4paper,12pt]{article}
\usepackage{nicematrix}
% \usepackage{listings}
% \lstdefinestyle{verbatim}{basicstyle=\ttfamily,language={[LaTeX]TeX}}
\usepackage{minted}
\makeatletter
%
% Similar to \EscVerb.
%
% \EscMintinline[options]{<language>}{<backslash-escaped text>}
%
% copied from https://github.com/gpoore/minted/issues/288#issuecomment-756418475
\def\EscMintinline{%
\FVExtraRobustCommand
\RobustEscMintinline
\FVExtraUnexpandedReadOArgMArgEscVArg}
\NewExpandableDocumentCommand \FVExtraUnexpandedReadOArgMArgEscVArg { o m m } {%
\IfNoValueTF{#1}
{\FVExtraAlwaysUnexpanded
{\FVExtraUnexpandedReadOArgMArgEscVArg{#2}{#3}}}
{\FVExtraAlwaysUnexpanded
{\FVExtraUnexpandedReadOArgMArgEscVArg[#1]{#2}{#3}}}%
}
\newrobustcmd\RobustEscMintinline[2][]{%
% similar to \mintinline
\begingroup
\setboolean{minted@isinline}{true}%
\minted@configlang{#2}%
\setkeys{minted@opt@cmd}{#1}%
\minted@fvset
\begingroup
\@ifnextchar\bgroup
{\FVExtraDetokenizeREscVArg{\minted@inline@iii}}%
{\PackageError{minted}%
{\string\EscMintinline\space delimiters must be paired curly braces in this context}%
{Delimit argument with curly braces}}}
\makeatother
\begin{document}
% \lstinline[style=verbatim]|\mathcal| abc
\begin{NiceTabularX}{\textwidth}[hvlines]{*{11}X[c, t]}
\Block{1-}{\EscVerb{\$\\mathcal{L}\\{f\\}(s) = \$}}
& & & & & & & & & & \\
\Block{1-}{\EscMintinline{tex}{\$\\mathcal{L}\\{f\\}(s) = \$}}
& & & & & & & & & & \\
\end{NiceTabularX}
\end{document}
v2.7更新minted
(2022-12-12)
minted
v2.7 重新实现了\mintinline
,使旧命令\EscMintinline
不兼容。由于 v2.7 引入了(非用户命令)\RobustMintInline
,我决定使用大写的 i 来制作修订后的命令。所以现在它被称为\EscMintInlint
。
更新实验实施示例\EscMintInline
% !TeX document-id = {4f64184d-77b8-4fbf-a0e2-f523f935f052}
% !TeX TXS-program:compile = txs:///pdflatex/{%.tex} -shell-escape "%.tex"
\documentclass[a4paper,12pt]{article}
\usepackage{nicematrix}
% \usepackage{listings}
% \lstdefinestyle{verbatim}{basicstyle=\ttfamily,language={[LaTeX]TeX}}
\usepackage{minted}
\makeatletter
%
% Similar to \EscVerb.
%
% \EscMintinline[options]{<language>}{<backslash-escaped text>}
%
% copied from https://github.com/gpoore/minted/issues/288#issuecomment-1501540637
\def\EscMintInline{%
\FVExtraRobustCommand
\RobustEscMintInline
\FVExtraUnexpandedReadOArgMArgEscVArg}
\NewExpandableDocumentCommand \FVExtraUnexpandedReadOArgMArgEscVArg { o m m } {%
\IfNoValueTF{#1}
{\FVExtraAlwaysUnexpanded
{\FVExtraUnexpandedReadOArgMArgEscVArg{#2}{#3}}}
{\FVExtraAlwaysUnexpanded
{\FVExtraUnexpandedReadOArgMArgEscVArg[#1]{#2}{#3}}}%
}
% similar to \RobustMintInline
\newrobustcmd{\RobustEscMintInline}[2][]{%
\ifbool{FVExtraRobustCommandExpanded}%
{\@ifnextchar\bgroup
{\FVExtraDetokenizeREscVArg{\RobustMintInlineProcess{#1}{#2}}}%
{\PackageError{minted}%
{Inline delimiters must be paired curly braces in this context}%
{Use curly braces as delimiters}}}%
{\FVExtraDetokenizeREscVArg{\RobustMintInlineProcess{#1}{#2}}}}
\makeatother
\begin{document}
% \lstinline[style=verbatim]|\mathcal| abc
\begin{NiceTabularX}{\textwidth}[hvlines]{*{11}X[c, t]}
\Block{1-}{\EscVerb{\$\\mathcal{L}\\{f\\}(s) = \$}}
& & & & & & & & & & \\
\Block{1-}{\EscMintInline{tex}{\$\\mathcal{L}\\{f\\}(s) = \$}}
& & & & & & & & & & \\
\end{NiceTabularX}
\end{document}