5

I've been trying to make the line numbers appear outside the colored frame in my output code blocks, but I also want everything (numbers included) to be inside the margins specified by the geometry package, which text paragraphs obey. My minted setup looks like this:

\documentclass[12pt]{article}

\usepackage{geometry}
\geometry{a4paper,
twoside,
left=30mm,
right=25mm,
top=25mm,
bottom=25mm}

\usepackage[section]{minted} % For code blocks
\newenvironment{code}{\captionsetup{type=listing}}{}

% ...

\begin{document}

\begin{code}
    \inputminted[
    xleftmargin=2em,
    %framexleftmargin=1.5em,
    bgcolor=lightgray!10,
    autogobble=true,
    numbers=left,
    frame=single,
    framesep=2mm,
    baselinestretch=1.2,
    fontsize=\footnotesize,
    breaklines=true]{verilog}{../UVMF_TFM/TFM_src/uvmf_tutorials/generator_tutorial_stepbystep/uvmf_template_output/project_benches/alu/rtl/verilog/alu.v}%
    \caption{Fuentes en \textit{Verilog} de una ALU}\label{dut_alu_rtl}%
\end{code}

A sample paragraph to check alignment: The simulation bench is composed of top level elements that are not generally intended to be reusable horizontally nor vertically. It defines test level parameters, the top level modules, top level sequence and top level UVM test. It also includes derived sequences and tests used to implement additional test scenarios\ldots%

% ...
% ...

\end{document}

The resulting output is like this:

Current output

But I would like it to be like this other one (but with the line numbers inside margin!):

Desired output with wrong alignment on numbers

I have tried with the framexleftmargin you can see commented out in the declared minted environment options, but this option belonged to another environment so of course it didn't compile. The second image does not have any xleftmargin or anything used, just default alignment (aside from numbers, coloring...)

Assuming it's possible, how could I get it right? Thanks in advance!

1 Answer 1

5

You could use tcolorbox to customize your listing as you like.

The advantage, compared to a framed box, is that the tcolorbox can also be breakable.

Edit: if you have more than one language, you can set two parameters: one for the language and the other for the file name.

\documentclass[12pt]{article}
\usepackage{geometry}
\geometry{a4paper,
    twoside,
    left=30mm,
    right=25mm,
    top=25mm,
    bottom=25mm}

\usepackage{caption}

\usepackage[section]{minted} % For code blocks
\newenvironment{code}{\captionsetup{type=listing}}{}

\usepackage[many]{tcolorbox}
\tcbuselibrary{minted}
\newtcbinputlisting{\mycode}[2]{%
  listing engine=minted,
  minted language={#1},
  listing file={#2},
  minted options={
    xleftmargin=2em,
    autogobble=true,
    numbers=left,
    numbersep=2em,
    baselinestretch=1.2,
    fontsize=\footnotesize,
    breaklines=true
    },
  listing only,
  breakable,
  enhanced jigsaw,
  colframe=black,
  sharp corners,
  boxrule=1pt,
  colback=lightgray!10,
  left=-1.5em,
  left skip=2em,
  width=\linewidth-2em
}

% The following code is only to create alu.v and myscript.py, of course, you do not need it in your actual document, because you already have the files
\usepackage{filecontents}
\begin{filecontents*}{alu.v}
//Example from https://it.wikipedia.org/wiki/Verilog
module toplevel(clock, reset);
 input clock;
 input reset;

 reg ff1;
 reg ff2;

 always @ (posedge reset or posedge clock)
 if (reset)
   begin
     ff1 <= 0;
     ff2 <= 1;
   end
 else
   begin
     ff1 <= ff2;
     ff2 <= ff1;
   end
endmodule
\end{filecontents*}
\begin{filecontents*}{myscript.py}
# code from https://www.learnpython.org/en/Hello,_World!
x = 1
if x == 1:
    # indented four spaces
    print("x is 1.")
\end{filecontents*}

\begin{document}
\begin{code}
\caption{Fuentes en \textit{Verilog} de una ALU}\label{dut_alu_rtl}%
\mycode{verilog}{alu.v}
\end{code}

A sample paragraph to check alignment: The simulation bench is composed of top level elements that are not generally intended to be reusable horizontally nor vertically. It defines test level parameters, the top level modules, top level sequence and top level UVM test. It also includes derived sequences and tests used to implement additional test scenarios\ldots
\begin{code}
\caption{This is a  \textit{python} code}\label{py}%
\mycode{python}{myscript.py}
\end{code}
\end{document}

enter image description here

2
  • Thanks for your answer, I will try it! Just one thing. Does it work for different syntax highlightings? Like, if in the same document I want to use both \minted{verilog}{...}, \minted{python}{...} and/or their \inputminted equivalents. I see that you specify the language once in the preamble, but I would like to use different languages on demand if possible, with all the other options set up once.
    – DavSanchez
    Commented May 2, 2019 at 22:12
  • 1
    @DavSanchez Just use two parameters, one for the language and the other for the file name. See my renewed answer. Thank you for accepting it!
    – CarLaTeX
    Commented May 3, 2019 at 5:27

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .