Usually when adding code in a LaTeX
document, I use the listings
package. When trying to modify and personalise styles in listings, @1010011010 suggested to use the minted
package.
To be quite frank, I was impressed! I've managed to install it and make it work according to my needs and taste. I've added line numbers and used the mdframed
package to add colored background that breaks along pages. At the moment I am trying to break a line when it's too large to fit within the \textwidth
. I used @MarcoDaniel 's answer on Box around minted environment to break the lines but it's not working as expected.
My code is
\documentclass{book}
\usepackage[]{minted}
\usepackage{tcolorbox}
\usepackage{lineno}
\usepackage{mdframed}
\def\gobble#1{}
\renewcommand\DeleteFile[1]{}
\usepackage{xparse}
\ExplSyntaxOn
\box_new:N \l_fvrb_box
\tl_new:N \l_fvrb_tl
\RenewDocumentCommand \FancyVerbFormatLine { m }
{
\hbox_set:Nn \l_fvrb_box { #1 }
\dim_compare:nNnTF { \box_wd:N \l_fvrb_box }>{ \linewidth }
{%box to big
\tl_set:Nn \l_fvrb_tl { #1 }
\fvrb_use_tl:N \l_fvrb_tl
}
{%box fits
\box_use:N \l_fvrb_box
}
}
\cs_new:Npn \fvrb_use_tl:N #1
{
\group_begin:
\null\hfill\vbox_set:Nn \l_fvrb_box
{\hsize=\linewidth
\renewcommand\thelinenumber
{
\ifnum\value{linenumber}=1\relax\else
$\rightarrow$
\fi
}
\begin{internallinenumbers}
\advance\hsize by -2em
\hspace*{-2em}\tl_use:N #1
\end{internallinenumbers}
}
\box_use:N \l_fvrb_box
\group_end:
}
\ExplSyntaxOff
\usepackage{etoolbox}
\usepackage{xcolor}
\definecolor{lbcolor}{rgb}{0.9,0.9,0.9}
\usepackage[margin=2.8cm]{geometry}
\begin{document}
\section{Section}
\begin{minted}[linenos=true,bgcolor=lbcolor,numberblanklines=true,showspaces=false]{java}
int slotADC = 22;// Change CAMAC controller's backpanel jumper, accordingly
int count = 0;
int[] data = new int[1];
int[] offset = {0, 0};
int[] in = {1, 2};
int[] ones = {1,1};
report.setProperty("INSERT", "["+now.format(new Date())+"] Acquisition Started\n");
report.setProperty("INSERT", "["+now.format(new Date())+"] I am set to read "+evSize+" detectors.\n");
Initialize();
\end{minted}
\begin{mdframed}[linecolor=black, topline=true, bottomline=true,
leftline=false, rightline=false, backgroundcolor=lbcolor,userdefinedwidth=\textwidth]
\begin{minted}[fontsize=\normalsize, linenos, frame=lines]{java}
import java.awt.event.*;//for ActionListener
public class Runtime implements KmaxRuntime {
KmaxToolsheet tlsh;//Store a reference to the toolsheet environment
KmaxWidget wSlot;
KmaxWidget wAdd;
KmaxWidget wData;
KmaxWidget dataField;
KmaxWidget countField;
KmaxWidget setSRQ;
KmaxDevice dev;
KmaxHist hist1,hist2,hist3,hist4,hist5,hist6, histoLowX, histoLowY;
KmaxHist dee1;
KmaxWidget report;
KmaxWidget liveTime;
KmaxWidget realTime;
KmaxWidget deadTime;
KmaxWidget progress;
KmaxWidget xcombo, ycombo, offsetButton;
KmaxWidget combo, checkBoxCalib;
/** ADC Levels */
KmaxWidget lld;
KmaxWidget setLLD;
KmaxWidget getLLD;
KmaxWidget lldText;
KmaxWidget uld;
KmaxWidget setULD;
KmaxWidget getULD;
KmaxWidget uldText;
/** Calibration - Check Box(use calibration) */
boolean eventFileOn = true;
int slotADC = 22;// Change CAMAC controller's backpanel jumper, accordingly
int count = 0;
int[] data = new int[1];
int[] offset = {0, 0};
int[] in = {1, 2};
int[] ones = {1,1};
int points = 10;
int[] region = new int[points];
int timeTicks;
int totalTime;
int LiveTime = 0;
int totcount;
int evType = 1; //The Type ID for these kinds of events
SimpleDateFormat today = new SimpleDateFormat("dd/MM/yyyy");
SimpleDateFormat now = new SimpleDateFormat("HH:mm:ss:SSS");
Map<String,KmaxHist> histograms = new HashMap<String,KmaxHist>();
static final int evSize = 6;//The num of parameters per event of this type
static final int BUF_SIZE = evSize*1000;// Buffer size
static final int LP_MEM_TOP = 0xFFFF00;//Memory size 16MB
static final int READ_START = LP_MEM_TOP - BUF_SIZE;//We start the read/write pointer 1 buffer before the end
\end{minted}
%\inputminted[fontsize=\normalsize, linenos, frame=lines]{java}{DAQ.java}%[linenos=true,bgcolor=lbcolor]{java}{DAQ.java}
\end{mdframed}
\end{document}
Line breaking seems to be working fine when using it's built in option for colored background bgcolor
.
This isn't the case when using mdframed
. For instance text jumps out of the frame and there's a rather annoying identation when detecting a line break. Take a look for instance lines 39 and 60 in the following image
Any idea on how can this be fixed?
minted
has abreaklines
option inspired by the answer you referenced. It seems to work fine with your example. The development version also has some options for customizing the break symbol and indent.minted.sty
from GitHub and put it in the same directory as your document, it will be used for that document. That would be a good way to check and see if it works for you. Longer term, you could install in texmf-local (see this). Or you could replace theminted.sty
in/texmf-dist/tex/latex/minted
(though that might be frowned on, since it putstexmf-dist
out of sync with the repository).breaklines=true
in the optional arguments but it seems that it's not recogized...minted.sty
(not the release download)? Are you usingbreaklines
as an optional argument for theminted
environment?