Typesetting Code in LaTeX

Since I recently pulled a few all-nighters to prepare code slides to teach my students R and they were less than enthusiastic, I thought I’d use the opportunity to at least write up a post on how to display code in LaTeX for you guys. It focuses on displaying the code for teaching, writing tutorials and so on. So the integration of code in beamer will be dominant, or at least the example on which I demonstrate things. Of course, you can also use the tips to just display it in a ‘normal’ document.

When I thought about it, I realized there are actually so many ways of displaying code using LaTeX packages. So I’ll start with the most basic and then go on to the more advanced ones 😉  You can find the example document as a template here on Overleaf. It uses the beautiful metropolis beamer theme, which is worth looking into as well.

code-listings1
Be sure to check out the template on Overleaf.

The \texttt{} command

This one isn’t a verbatim way to express code, but it will change the font to typewriter, so it ‘looks like code’. However, in these short bits of code, you will have to use escape sequences for reserved characters. Furthermore, it (shockingly!) is not natively included in beamer, so you have to add it yourself in the preamble if you want it (as demonstrated in the template). How can one write about code without over-using \texttt{}?

I make massive use of \texttt{} when ‘talking’ about code or, you know, writing explanatory text sequences. It is especially useful for bits of code where there is no excessive (or none at all) use of escape sequences. Then it is a really handy way to quickly typeset code. Once you have lots of reserved characters, you might be better of just using this next one.

The verbatim environment

This one really is a staple and pretty failsafe, but also doesn’t have code highlighting which you might want in most cases, apart from very short bits of code where highlighting isn’t important.

You can use verbatim as an environment for multiple lines of code which appear like a quote as a separate block in your text. In other cases, where you just want \texttt{} but without having to escape reserved characters, you might want to use the \verb|some code| command. You can use any characters as delimiters to denote beginng and end of code. To be more precise, a quote from the Overleaf site: “Any character, except letters and *, can be used as delimiter. ” So it can also be \verb+test+. The idea is that you can choose one which you will not need inside the code, as not to ‘confuse’ the enviroment.

A note about code in beamer

When you use code in beamer, make sure to always use the fragile option for the frames in question. This is necessary due to the way how beamer handles verbatim, apparently.

Also, beamer and code can generally can wreck quite some havoc. I have had problems with lots of KnitR where it would stop working for no reason, then the exact same code would work again later, etc. And there are lots of conflicts and bugs you need to know about. So if you run into problems here, don’t blame yourself. It might easily be beamer which is ‘responsible’ for some of the problems.

The lstlisting package

You can just use the lstlisting environment without configuring anything but then again, it will not have code highlighting which is kind of the point in typesetting code (most times at least).

However, I find that without setting your own colours for the listings, it still looks quite boring with the black and white hightlighting (bold vs. non-bold). You can configure it any way you like, can even define yourself which elements are to be hightlighted and how. But it is quite a lot of work if there isn’t something you like out of the box. I personally prefer minted and especially tcblisting – that is minted with tcolorbox (as far as I know, even though it’s called listing). It has these cute little boxes. I like boxes.

Using KnitR

When using knitr to run R in LaTeX, don’t forget to change the file extension from .tex to .Rtex so LaTeX gets a heads up that you want to use KnitR. Otherwise you’ll end up with jumbled code and might spend hours trying to find the mistake, since it isn’t actually in your code. (Just so you know, I speak from experience here 😉 It’s just so easy to forget to do this. Basically happens to me all the time…)

You can use KnitR out of the box on Overleaf. Note, however, that they might not have all the packages you might want. But if, like me, you just want to display code to teach coding, you can eval=FALSE the results (which will be error messages due to lacking packages) and let the students run the code themselves. Since my elaborated fancy didactical plan was not to share the code as a file but rather have them type it themselves from the presentations, to increase the learning effect. Disclaimer: It did not work out the way I had hoped it would 😉

The minted package

If you like fancy, minted probably is for you. It’s kind of like lstlisting but with more beautiful colours and preset options. Give it a try. It’s probably the go-to solution.

Minted and tcblistings

tcblistings use minted and tcolorbox to create highlighted code listings in cute boxes. For me, that’s just too good to be true 😉 Note, however, that Beamer, tcblisting and enumitem are not compatible with each other. There is a bug I haven’t yet found a way around. So you can’t use customized enumerates oder itemize in the same presentation as these code listings! Sad, but true. But maybe there is a not-all-that-inconveniant way for you to keep presentation and code parts in separate sets of slides anyway. Here, it’s on you to get creative 😉

Generally, with the beamer package, there are actually quite a few things which don’t work once you want to use some less common features. So in case you encounter a bug and really can’t see how it’s your fault, web search if it’s a common error or known bug. Maybe there will be a way around it, maybe there won’t.

A workaround for this specific case would be to use beamer with lstlisting.

Wrapup

So, I hope this helped you and provided you with  an overview of possible ways of typesetting code in LaTeX, with a focus on presentations.

Best, the Ninja

Resources

Buy me coffee!

If my content has helped you, donate 3€ to buy me coffee. Thanks a lot, I appreciate it!

€3.00

I like LaTeX, the Humanities and the Digital Humanities. Here I post tutorials and other adventures.

One thought on “Typesetting Code in LaTeX

Leave a comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.