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
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}
This looks good, and I like the box, but I can’t figure out how to solve two problems:
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.
Some notes:
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.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}
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
Get help from others!
Recent Questions
Recent Answers
© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP