0

I need to create documents containing lots of small bits of R code, which are not to be sent to R via knitr. Ideally, it should work very much like code is formatted here at Stackexchange. It should be set in ttfamily and not break across lines. I'm not sure that a gray background is necessary. I'd prefer not to use \begin{environment} and \end{environment}, because that's cumbersome when the code is just a couple words.

It seems like the trickiest part might be that some special characters should loose their specialness. Specifically, "$" and "_", which are commonly used in R code shouldn't have to be escaped.

There is a solution to a similar question here, but that's taking the \begin{}\end{} approach, so it won't work here.

Something like \code{my code} would work better. I have the beginnings of a macro with this:

\newcommand{\nbcode}[1]{\mbox{\texttt{#1}}}

It'd be great if the solution to this is a package I haven't heard of.

Gregory
  • 595

1 Answers1

3

The simplest method offered by LaTeX is the \verb macro and the verbatim environment. So for example you can write \verb|print($hello)| and obtain print($hello). Note that instead of regular brackets, \verb's argument is delimited by whichever character you put right after it. This allows you to avoid escaping the delimiters by just choosing a delimiter that does not conflict with the contents.

You should also have a look at the standard packages for this kind of things:

  • verbatim
  • fancyvrb
  • listings
  • minted

These offer many different ways to customise the appearance of the code by offering boxes, colors, highlighting etc.

Bordaigorl
  • 15,135