TransWikia.com

Formatting minted code box in tcblisting

TeX - LaTeX Asked by kevin-robb on March 9, 2021

I am creating a code/strategy reference document for my student organization to be built on by groups of students into the future, and trying to make it simple and readable for those who will be adding to it and updating it later. I’ve been representing code in the following way, which is fine, but a long message and I wanted to make it something shorter.

begin{minted}[mathescape, linenos, fontsize=footnotesize]{python}
class MyClass:
    def my_function():
        # my code
end{minted}

This produces the display shown in the image

What the the first code block produces

I know that minted is a verbatim environment, so the regular newenvironment{codeblock}{...}{...} command would not really work. When I tried to use it, it seemed to work okay but the end{codeblock} would not be recognized, causing the whole rest of the document to be in the code style.

I found this post, which shows a way to use minted with the newtcblisting command in the tcolorbox package, and it certainly looks better, but has a couple problems as well. I define the environment:

usepackage{minted} usepackage{tcolorbox} tcbuselibrary{minted}
newtcblisting{codeblock}[2][]{
  minted options={mathescape, linenos,fontsize=footnotesize},  
  minted language=#2,
  #1
}

And I then use it,

begin{codeblock}{python}
class MyClass:
    def my_function():
        # my code
end{codeblock}

What the second code block produces

This looks good, and I like the box, but I can’t figure out how to solve two problems:

  1. The line numbers are overlapping the left edge of the box. Is there a way to shift all the code text (including the line numbers) to the right?
  2. The raw code is displayed as text in the lower half (and seems to switch into math mode after the underscore), which did not happen in the post I referenced and linked above. How can I prevent this from displaying, and have only the nice-looking code in the box?

Any help would be appreciated with solving these specific issues, or even just a way to define a custom environment that works with minted. I’ve found a few posts on here but nothing that’s worked.

One Answer

Some notes:

  • handling line number overlapping: either use minted option numbersep=<dimension> to reduce the separation between raw code and line numbers, or use tcolorbox option left=<dimension> to enlarge the left inner margin of the colored box. In the following example, numbersep=5pt is used.
  • show raw code only: tcolorbox option listing only.
documentclass{article}
usepackage{tcolorbox}
tcbuselibrary{hooks, minted}

newtcblisting{codeblock}[2][]{
  minted options app={mathescape, linenos, fontsize=footnotesize, numbersep=5pt},  
  minted language=#2,
  #1,
  listing only
}

begin{document}
begin{codeblock}{python}
class MyClass:
    def my_function():
        # my code
end{codeblock}
end{document}

enter image description here

PS: the minted options option has default value tabsize=2,fontsize=small, breaklines,autogobble. When you use minted options={...}, the default value is overwritten. Hence another option minted options app={...} from hooks library is used, which appends new options to the default ones.

Correct answer by muzimuzhi Z on March 9, 2021

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP