Any idea on how to reproduce the code in the first image?
Yes, I've got a few ideas, see below.
However, highlighting numbers (including hexadecimal expressions, such as 0xFFFF00
) in a robust way is, though not impossible, notoriously difficult. If you're determined to implement a solution, you could use my answer to How to highlight all words of the form [0-9][A-Za-z0-9]* immediately following an equal sign? as a starting point.
\documentclass{article}
\usepackage[T1]{fontenc}
\usepackage[scaled=0.85]{beramono}
\usepackage{listings}
\usepackage{textcomp}
\usepackage{xcolor}
\usepackage{lstautodedent}
\definecolor{listinggray}{gray}{0.9}
\definecolor{lbcolor}{rgb}{0.9,0.9,0.9}
\lstdefinestyle{JavaStyle}{
language=Java, % choose the language of the code
deletekeywords={new,public},
keywords=[2]{HashMap,Map,SimpleDateFormat,String},
keywords=[3]{getKmaxDevice,getKmaxWidget,getKmaxHist,init},
basicstyle=\scriptsize\ttfamily,
keywordstyle=\color[RGB]{69,97,189},
keywordstyle=[2]{\color{cyan}},
keywordstyle=[3]\color[RGB]{137,77,155},
commentstyle=\itshape\color{green!60!black},
moredelim=[l][\itshape\color{gray}]{//}, %<--- overrides line-comment style
stringstyle=\color[RGB]{192,8,8},
numberstyle=\itshape\color{yellow!50!black},
% backgroundcolor=\color{lbcolor},
tabsize=4,
% rulecolor=,
upquote=true,
aboveskip={1.5\baselineskip},
columns=fixed,
showstringspaces=false,
extendedchars=false,
breaklines=true,
prebreak = \raisebox{0ex}[0ex][0ex]{\ensuremath{\hookleftarrow}},
frame=single,
numbers=left,
showtabs=false,
showspaces=false,
showstringspaces=false,
autodedent,%<--- removes indentation
}
\begin{document}
\begin{lstlisting}[style=JavaStyle]
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 int 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
int[] blkData = new int[BUF_SIZE];
/**
* The 'init' method is executed at compile time.
*/
public void init(KmaxToolsheet toolsheet) {
tlsh = toolsheet;//Save this reference for use in the toolsheet
dev = tlsh.getKmaxDevice("DEV1");
dataField = tlsh.getKmaxWidget("$R_DATA");
countField = tlsh.getKmaxWidget("$R_COUNT");
liveTime = tlsh.getKmaxWidget("LIVE_TIME");//Live Time
realTime = tlsh.getKmaxWidget("REAL_TIME");//Real Time
deadTime = tlsh.getKmaxWidget("DEAD_TIME");//Dead Time
hist1 = tlsh.getKmaxHist("DATA1");
hist2 = tlsh.getKmaxHist("DATA2");
hist3 = tlsh.getKmaxHist("DATA3");
hist4 = tlsh.getKmaxHist("DATA4");
hist5 = tlsh.getKmaxHist("DATA5");
hist6 = tlsh.getKmaxHist("DATA6");
histoLowX = tlsh.getKmaxHist("HIST_LOW_X");
histoLowY = tlsh.getKmaxHist("HIST_LOW_Y");
histograms.put("DATA1", hist1);
histograms.put("DATA2", hist2);
histograms.put("DATA3", hist3);
\end{lstlisting}
\end{document}
It turns out that I am not able to sleep. As promised, here is a version using tcolorbox
and listings
.
\documentclass[a4paper,11pt]{article}
\usepackage[top=19mm, left=12.925mm, right=12.925mm, bottom=19mm]{geometry}
\usepackage{xcolor}
\usepackage{listings}
\usepackage{xcolor-solarized}
\lstdefinestyle{mystyle}{
basicstyle = \ttfamily\color{solarized-base00}, % -- Making the font monospaced and small
%backgroundcolor=\color{solarized-base3}, % -- Setting the background colour to solarized-base3
breaklines=true, % -- Allows automatic line breaking
captionpos=b, % -- Caption position at bottom of listing
commentstyle = \color{solarized-base1}, % -- Sets comments to solarized-green
escapeinside = {\%*}{*}, % -- Useful for adding LaTeX within the code
emphstyle =\bfseries\color{solarized-red}, % -- Emphasized text style to red
identifierstyle=\color{solarized-base00}, % -- Style for Identifiers
keepspaces=true, % -- Keeps Spaces in text, useful for keeping identation of code.
keywordstyle = \color{solarized-cyan}\bfseries, % -- Keyword style
numbers=left, % -- Line numbers on the left
numbersep=0.5em, % -- How far away should line numbers be from code
numberstyle=\normalfont\footnotesize\color{solarized-violet}, % -- Line Number font style, size and colour
showspaces=false, % -- Do not show spaces everywhere by adding particular underscores
showtabs=fase, % -- Do not show tabs within strings adding particular underscores
stringstyle=\color{solarized-blue}, % -- String style
tabsize=2, % -- Setting the tab size to 2 spaces.
}
\newcommand\sideline{
\draw[line width=2em,solarized-base2]
([xshift=1em+1pt]frame.north west) --
([shift={(1em+1pt,1pt)}]frame.south west);
}
\usepackage[most]{tcolorbox}
\newtcblisting{mylisting}{%
nobeforeafter,
colback=solarized-base3,
colframe=solarized-blue,boxrule=0pt,leftrule=1pt,bottomrule=1pt,
boxsep=0pt,left=2em,top=-2mm,right=0pt,bottom=-2mm,arc=0pt,enhanced jigsaw,breakable,
listing options={style=mystyle},listing only,
overlay unbroken and first={\sideline},
overlay middle and last={\sideline}
}
\newtcbinputlisting{\myinputlisting}[2][]{%
listing file={#2},
nobeforeafter,
colback=solarized-base3,
colframe=solarized-blue,boxrule=0pt,leftrule=1pt,bottomrule=1pt,
boxsep=0pt,left=2em,top=-2mm,right=0pt,bottom=-2mm,arc=0pt,enhanced jigsaw,breakable,
listing options={style=mystyle},listing only,
overlay unbroken and first={\sideline},
overlay middle and last={\sideline},
#1}
\usepackage{filecontents}
\begin{filecontents*}{myfile.txt}
for i:=maxint to 0 do
begin
{ do nothing }
end;
Write(’Case insensitive ’);
WritE(’Pascal keywords.’);
\end{filecontents*}
\begin{document}
\begin{mylisting}
for i:=maxint to 0 do
begin
{ do nothing }
end;
Write(’Case insensitive ’);
WritE(’Pascal keywords.’);
\end{mylisting}
\myinputlisting{myfile.txt}
\end{document}
Best Answer
\lstinputlisting{}
should not be inside a\begin{lstlisting}
and\end{lstlisting}
pair. Remove the begin and end and it should work as expected.