0

I am using R Markdown and Latex with memoir to compile a book to pdf. I am using lstsetfor the normal code blocks while the inline code is converter and uses \lstinline. I would like to globally add a border (like the one in the photo) around all inline code without affecting the normal code examples.

An example of the desired inline code style (this is just HTML no tex)

EDIT: 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}
7
  • 1
    This does not seem to be related to memoir at all so I suggest removing the memoir tag. If you want help you need to provide example code that others can test. Given that this is via R markdown (which not many people here know anything about, it is probably best to provide examples of the latex code generated from R Markdown
    – daleif
    Jun 18 at 12:53
  • @daleif I removed the memoir tag 👍 In terms of the code produced it is simply \lstinline{} tags around the inline code.
    – Dionysis
    Jun 18 at 12:56
  • Thanks. You should provide a document that uses \lstinline than. People are much more inclined to help if they have code to work with.
    – daleif
    Jun 18 at 13:07
  • @daleif the problem is that I am compiling from RMarkdown to pdf so any code would be a hello world latex example that uses a \lstinline tag and that is all 🤷‍♂️
    – Dionysis
    Jun 18 at 13:14
  • 1
    @daleif I've added the MWE 👍
    – Dionysis
    Jun 18 at 14:21
0

This was a tricky one and the (specific to RMarkdown) solution was to use tcolorbox as per @daleif's suggestion in combination with a pandoc lua filter to apply the box globally.

% ------------------------------------------------------------
% 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^
  }

the lua filter:

if FORMAT:match 'latex' then
  function Code (element)
    local text = element.text
    return {
      pandoc.RawInline(
        'latex', 
        '\\scinline{' .. text .. "}"
      ),
    }
  end
end

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.