1

I am writing a paper on blockchain and I saw a style that I really liked. It is to put the code on a black background (my sheet is divided into two columns). The problem is that it is the first paper I am writing and I don't know very well how to put the code with the background in gray (besides, it would have to have the same width as the column).

I put a picture in case it can be of help:

enter image description here

I tried to execute this code (it's just an example):

\documentclass{article}

\usepackage{listings}
\usepackage{tcolorbox}
\usepackage{blindtext}  % For dummy text

\lstset{
    keywordstyle=\color{blue}\bfseries,
}

\begin{document}
    \blindtext
    \begin{tcolorbox}[boxrule=0.5pt,sharp corners]
    \begin{lstlisting}[language=Python, gobble=12]
    function calculateFitness() public{
        for(uint j = 0; j < population.length; j++){
            uint[] memory assignments = new uint[](agents.length);
            for(uint i = 0; i < population[j].getGenesLenght(); i++){
                uint[] memory pref;
                // We get preferences for current agent
                (,,pref) = agents[population[j].getGene(i)].getAgentInfo(); 
                assignments[population[j].getGene(i)] += pref[i];
            }
            population[j].setFitness(getMin(assignments));
        } 
    }
    \end{lstlisting}
    \end{tcolorbox}
    \blindtext
\end{document}

enter image description here

and then I got few questions:

1.- How could I fix the text so that it does not stand out of the box? 2.- It seems LaTeX doesn't have Solidity languague (Ethereum blockchain) so I'd like to put different colors (like the pic above, the first one) but I can't write \textcolor{blue} inside the listings labels. 3.- I don't understand why the first word has been removed: the code starts with "function calculateFitness()...", but the pic only shows "calculateFitness()..."

8
  • 1
    You specified gobble=12, so the first twelve characters of each line is eaten. Commented Apr 30, 2021 at 17:24
  • Oh, true! My bad.
    – Joncarre
    Commented Apr 30, 2021 at 17:27
  • 1
    try also adding the basicstyle=\small key to lstlisting, this will set the code in smaller font, making it possible to fit more in a fixed width. If it still is too wide: it is your job to format the input in a way that works with the syntax of the language and display nicely. For python it can be a it annoying. But for braces-semicolon languages like solidity, you can insert line breaks, right? Alternatively you can try breaking out of two-column mode and have the code listing be full width. Commented Apr 30, 2021 at 17:32
  • 1
    As for the package not supporting solidity, please look at the manual (section 3.2) ctan.org/pkg/listings?lang=en If you have a list of the common language keywords, making it known to the package takes just a few lines of code. Commented Apr 30, 2021 at 17:34
  • 1
    See section 1.3 of the manual. You can change the font size/style for the basic code, as well as specified how keywords and comments will be displayed. Commented Apr 30, 2021 at 21:08

1 Answer 1

2

enter image description here

Here's one way to do it using the listings package for typesetting the code and the tcolorbox package for the gray box (which just happens to be the default colour for a tcolorbox). Featuring some hastily written example code in python. There is much more that you could do with both listings and tcolorbox to control how the output looks but this should get you started. This also works if you use the twocolumn option but be careful as the code doesn't wrap automatically so is likely to overrun into the next column.

\documentclass{article}

\usepackage{listings}
\usepackage{tcolorbox}
\usepackage{blindtext}  % For dummy text

\lstset{
    keywordstyle=\color{blue}\bfseries,
}

\begin{document}
    \blindtext
    \begin{tcolorbox}[
        boxrule=0pt,
        sharp corners
    ]
        \begin{lstlisting}[language=python, gobble=12]
            def fib(n: int):
                if n < 0:
                    raise ValueError
                else if n == 0 or n == 1:
                    return 1
                else:
                    return fib(n - 1) + fib(n - 2)
        \end{lstlisting}
    \end{tcolorbox}
    
    \blindtext
\end{document}
4
  • What is the difference between loading listings and tcolorbox and just loading tcolorbox with the listings library? Commented Apr 29, 2021 at 18:25
  • 1
    For the purpose of this I've just tried and both work the same, but I wasn't even aware that listings could be loaded in that way before this comment so I'm probably not the person to ask.
    – Willoughby
    Commented Apr 29, 2021 at 18:30
  • No worries, I was just wondering Commented Apr 29, 2021 at 21:36
  • @Willoughby I updated the post and maybe you could help me a little more. I really appreciate it. Thanks in advance.
    – Joncarre
    Commented Apr 30, 2021 at 17:16

You must log in to answer this question.

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