我想使内联代码与其周围的文本更容易区分,并且我认为使用背景可能会起作用(就像在 stackexchange 页面上所做的那样)。
由于我对其他代码示例使用了 listings 包,因此我尝试了 listings 的内联版本,但是 key=value 对似乎在内联版本中不起作用(尽管文档似乎表明它们应该起作用)。
完整示例:
\documentclass{article}
\usepackage{color}
\usepackage{listings}
\begin{document}
This is a test where \lstinline[backgroundcolor=\color{yellow}]{A=1} should have a yellow background.
This, on the other hand, actually works:
\begin{lstlisting}[backgroundcolor=\color{yellow}]
A = 1
\end{lstlisting}
\end{document}
问题:
- 能用清单来实现吗?怎么做?
- 您还使用哪些其他方法来提高内联逐字的可区分性?
答案1
我不太熟悉该lstlistings
包中与颜色和突出显示相关的大多数选项。但是,包\colorbox
中定义的宏color
带有两个参数(颜色和要放置在彩色框中的项目),可以很好地突出显示一段内联文本。以下 MWE 直接构建在您的代码上:
\documentclass{article}
\usepackage{color,listings}
\begin{document}
This is to test whether \colorbox{yellow}{\lstinline{A=1}}
has a yellow background.
\end{document}
附录: 作为MartinScharrer 指出\colorbox
,如果命令的参数\lstinline
包含“真实”逐字材料(其中可能包含一些 TeX“特殊”字符,例如%
、_
、&
等等),则该命令将无法完成工作。对于这种情况,您应该加载真實的盒子打包并使用其\Colorbox
宏:
This is to test whether \Colorbox{yellow}{\lstinline{A=@#$%^&*()1}} has a yellow background.
答案2
您还可以使用以下解决方案如何重新定义 \lstinline 以自动突出显示或绘制所有内联代码片段周围的框架?
突出显示的代码来自突出显示代码列表中的文本,同时保持语法突出显示
已知的问题:
- 这旨在用于短的内联代码片段不是跨越换行符。
参考:
- 请参阅后续问题:尝试突出显示“\lstinline”时页面边界出现问题
代码:
\documentclass{article}
\usepackage{etoolbox}
\usepackage{atbegshi,ifthen,listings,tikz}
% 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}
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}
答案3
也可以声明一个新命令(或文档命令)\lsti
:
\documentclass{article}
\usepackage{color}
\usepackage{listings}
% The following doesn't work in all cases, as pointed out by matexmatics
% \newcommand{\lsti}[1]{\colorbox{yellow}{\lstinline|#1|}}
% This works in more cases, as pointed out by mbert
\NewDocumentCommand{\lsti}{v}{\colorbox{yellow}{\lstinline|#1|}}
\begin{document}
This is a test where \lsti|A=1| should have a yellow background.
It should also work with \lsti|A=1%| or \lsti|A={}| or \lsti|{{{|.
\end{document}
答案4
您可以组合lua-el
(突出显示行)和piton
(用于信息列表的句法着色)。这两个包都需要使用 LuaLaTeX 进行编译。
可以使用键来允许换行break-lines-in-piton
。
\documentclass{article}
\usepackage{piton}
\usepackage{luacolor,lua-ul}
\LuaULSetHighLightColor{gray!15}
\usepackage{piton}
\begin{document}
\makeatletter
\NewDocumentCommand{\InlineCode}{m}{{\@highLight\piton{#1}}}
\makeatother
\PitonOptions{break-lines-in-piton,language = OCaml}
Here is an example in OCaml.
If you want to test whether a year is a leap year :
\InlineCode{let leap n = n mod 4 = 0 && ( n<=1582 || n mod 100 <> 0 || n mod 400 =0 ) ;;}
\end{document}
该软件包piton
能够与所支持的所有语言一起使用listings
。