我正在使用R Markdown
和Latex
来memoir
将一本书编译为 pdf。我使用lstset
来处理普通代码块,而内联代码是转换器并使用\lstinline
。我想在所有内联代码周围全局添加边框(如照片中的边框),而不影响普通代码示例。
編輯:MWE
\documentclass{article}
\usepackage{listings}
\usepackage[framemethod=tikz]{mdframed}
\lstset{
basicstyle=\linespread{1}\footnotesize\ttfamily,
extendedchars=true,
showspaces=false,
xleftmargin=5pt,
framexleftmargin=17pt,
framexbottommargin=0pt,
showtabs=false,
}
\surroundwithmdframed[
innerleftmargin=0pt,
innerrightmargin=0pt,
innertopmargin=5pt,
innerbottommargin=0pt,
linewidth= 0.8pt,
linecolor= gray,
roundcorner=0pt
]{lstlisting}
\begin{document}
This is an example of some inline code that appears like \lstinline{Env.linen} and another one like \lstinline{Out.ar}. And this is a normal code block:
\begin{lstlisting}
(
SynthDef(\mySynth4, {
var signal, env; // declare variables
env = Env.linen(
attackTime: 0.05,
sustainTime: 0.5,
releaseTime: 1);
signal =
LFCub.ar(freq: 220, mul: 0.3) *
EnvGen.kr(envelope: env, doneAction: 2);
Out.ar([0,1], signal);
}).add;
)
g = Synth(\mySynth4);
\end{lstlisting}
\end{document}
答案1
这是一个棘手的问题,(特定于 RMarkdown 的)解决方案是tcolorbox
根据 @daleif 的建议结合使用 pandoc lua 过滤器来全局应用该框。
% ------------------------------------------------------------
% style all inline code with borders using tcolorbox
% I am using a lua filter to convert \lstinline to \scinline
\usepackage[most, breakable]{tcolorbox}
\DeclareTotalTCBox{\scinline}{ s v } {
verbatim,
sharp corners,
colupper=black,
colback=white,
arc=0.5mm,
size=fbox,
boxsep=1.3pt,
colframe=code-border} {
\lstinline[]^#2^
}
Lua 过滤器:
if FORMAT:match 'latex' then
function Code (element)
local text = element.text
return {
pandoc.RawInline(
'latex',
'\\scinline{' .. text .. "}"
),
}
end
end