4

I am preparing a tutorial for NetLogo, a programming language that does not have a predefined listings format. No problem to create one. However, I am trying to present the code inside a tcolorbox. This is what I have so far.

\documentclass[10pt]{article}

\usepackage{xcolor}
\usepackage{listings}
\usepackage[most]{tcolorbox}

\lstdefinelanguage{NetLogo}{
    breaklines=true,
    breakatwhitespace=true,
    alsoletter={-,?,.},
    morekeywords=[1]{to, to-report, end},
    keywordstyle=[1]\color[rgb]{0.25,0.5,0.35},
    morekeywords=[2]{ask, let, set, if, report, ifelse},
    keywordstyle=[2]\color{blue},
    morekeywords=[3]{not, n-of, color},
    keywordstyle=[3]\color[rgb]{0.6,0,0.8},
    comment=[l]{\;},
    commentstyle=\color[rgb]{0.75,0.75,0.75},
    string=[d]{"},
    stringstyle=\color{orange},
    morekeywords=[4]{1, 2, 3, 4, 5, 6, 7, 8, 9, 0, true, false},
    keywordstyle=[4]{\color{orange}},
}

\lstset{language=NetLogo}

\newtcolorbox[auto counter,number within=section]{snippetbox}[2][]{
  enhanced,
  skin=bicolor,
  overlay={\draw[dashed,blue!75!black] (segmentation.west)--(segmentation.east);},
  colback=blue!15!white,
  colbacklower=gray!10!white,
  colframe=blue!75!black,
  fonttitle=\bfseries,
  title=Snippet~\thetcbcounter: #2,#1}

\title{NetLogo code instructions example}
\author{}
\date{}

\begin{document}
\maketitle

This is a test document for the box structure for code snippet with instructions.

\section{First example}

\begin{snippetbox}{Adding behaviour}
Make various preparatory changes to interface:
\begin{enumerate}
\item Add to section L
\item Amend procedure
\end{enumerate}
\tcblower
\begin{lstlisting}
to setup-patches
  ask patches [ set popn 2000 + random 10000 ]
  ask n-of 3 patches [ set popn 1000000 * (3 + random 3) ]
  set max-popn max [popn] of patches
end
\end{lstlisting}
\end{snippetbox}

\end{document}

This works fine. However, I am forced to specify the lstlisting environment within the \tcblower section. I tried to put it in the tcolorbox specification and the code became plain text.

There will be about 30 of these boxes in the tutorial, all with identical format. Is there a way to use a custom language specification or should I just continue to do it this way?

4
  • In the tcolorbox manual there is how to make box plus listings envs, you need the listings tcolorbox library to be loaded, there is then also an extra tcolorbox option that you can pass næon your listings settings to, so no need for this dublication. However I've never tried that with a split tcolorbox, so I'm not sure if that is supported.
    – daleif
    Jul 20, 2018 at 19:01
  • Thanks for the comment. I know I can make a box with the listings, but I need listings to apply only to the lower segment. That's what I can't figure out how to do.
    – JenB
    Jul 21, 2018 at 0:24
  • Perhaps it would be an idea to make the title more precise then. As it sits now it is rather easy, until one actually read the boby of the question. That two part style ought to be a part of the title.
    – daleif
    Jul 21, 2018 at 6:08
  • okay. have done so
    – JenB
    Jul 21, 2018 at 9:44

1 Answer 1

5

tcolorbox was initially developed to type LaTeX+result boxes, therefore a tcblisting box, by default shows LaTeX code in one part and the result on the other part.

But it's also possible to define listing boxes where one part shows the code and the other part shows some other thing like regular text or images. That's why options listing and comment, comment and listing, listing and image and image and listing ar provided.

For this particular case you can use comment and listing where upper part is filled with some text and lower part whit a particular code fragment.

Folowing you'll find your code with some little changes:

  1. \newtcolorbox has been replaced for \newtcblisting
  2. A third parameter has been added to include the comment contents (previous upper part). See comment={#3} inside options list.
  3. Option comment and listing has been added
  4. Option listing options={language=NetLogo} has been included to apply NetLogo language format to the code
  5. In snippetbox environment, upper part has been included like thir parameter
  6. In snippetbox, tcblisting environment has been deleted in lower box.

That's all

\documentclass[10pt]{article}

\usepackage{xcolor}
\usepackage{listings}
\usepackage[most]{tcolorbox}

\lstdefinelanguage{NetLogo}{
    breaklines=true,
    breakatwhitespace=true,
    alsoletter={-,?,.},
    morekeywords=[1]{to, to-report, end},
    keywordstyle=[1]\color[rgb]{0.25,0.5,0.35},
    morekeywords=[2]{ask, let, set, if, report, ifelse},
    keywordstyle=[2]\color{blue},
    morekeywords=[3]{not, n-of, color},
    keywordstyle=[3]\color[rgb]{0.6,0,0.8},
    comment=[l]{\;},
    commentstyle=\color[rgb]{0.75,0.75,0.75},
    string=[d]{"},
    stringstyle=\color{orange},
    morekeywords=[4]{1, 2, 3, 4, 5, 6, 7, 8, 9, 0, true, false},
    keywordstyle=[4]{\color{orange}},
}

\lstset{language=NetLogo}

\newtcblisting[auto counter,number within=section]{snippetbox}[3][]{
  enhanced,
  skin=bicolor,
  overlay={\draw[dashed,blue!75!black] (segmentation.west)--(segmentation.east);},
  colback=blue!15!white,
  colbacklower=gray!10!white,
  colframe=blue!75!black,
  fonttitle=\bfseries,
  title=Snippet~\thetcbcounter: #2,
  comment and listing,
  comment={#3},
  listing options={language=NetLogo},
  #1}

\title{NetLogo code instructions example}
\author{}
\date{}

\begin{document}
\maketitle

This is a test document for the box structure for code snippet with instructions.

\section{First example}

\begin{snippetbox}{Adding behaviour}
{Make various preparatory changes to interface:
\begin{enumerate}
\item Add to section L
\item Amend procedure
\end{enumerate}}
to setup-patches
  ask patches [ set popn 2000 + random 10000 ]
  ask n-of 3 patches [ set popn 1000000 * (3 + random 3) ]
  set max-popn max [popn] of patches
end
\end{snippetbox}

\end{document}

enter image description here

1
  • Thank you. The comment and listing was the bit I hadn't been able to find.
    – JenB
    Jul 21, 2018 at 9:43

You must log in to answer this question.

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