3

I created a \lstdefinelanguage command for some JSON blocks I'd like to add into my document, I followed the answer to this question:

How to improve listings display of JSON files?

In the answer the answerer defines the language like so:

\usepackage{bera}% optional: just to have a nice mono-spaced font
\usepackage{listings}
\usepackage{xcolor}

\colorlet{punct}{red!60!black}
\definecolor{background}{HTML}{EEEEEE}
\definecolor{delim}{RGB}{20,105,176}
\colorlet{numb}{magenta!60!black}

\lstdefinelanguage{json}{
    basicstyle=\normalfont\ttfamily,
    numbers=left,
    numberstyle=\scriptsize,
    stepnumber=1,
    numbersep=8pt,
    showstringspaces=false,
    breaklines=true,
    frame=lines,
    backgroundcolor=\color{background},
    literate=
     *{0}{{{\color{numb}0}}}{1}
      {1}{{{\color{numb}1}}}{1}
      {2}{{{\color{numb}2}}}{1}
      {3}{{{\color{numb}3}}}{1}
      {4}{{{\color{numb}4}}}{1}
      {5}{{{\color{numb}5}}}{1}
      {6}{{{\color{numb}6}}}{1}
      {7}{{{\color{numb}7}}}{1}
      {8}{{{\color{numb}8}}}{1}
      {9}{{{\color{numb}9}}}{1}
      {:}{{{\color{punct}{:}}}}{1}
      {,}{{{\color{punct}{,}}}}{1}
      {\{}{{{\color{delim}{\{}}}}{1}
      {\}}{{{\color{delim}{\}}}}}{1}
      {[}{{{\color{delim}{[}}}}{1}
      {]}{{{\color{delim}{]}}}}{1},
}

The answerer uses the bera font, which makes the JSON look far more visually appealing. I'd like to copy this. The only problem is that when I add the \usepackage{bera} command all the text in my entire document is changed to this font, which I don't want. I only want the bera font to be used when I create a listing using the JSON language. What is the best way to do this?

UPDATE:

When I try \usepackage{beramono} as suggested by @Davislor I get this output:

enter image description here

The output I'd like looks like this, which I get when I use \usepackage{bera}:

enter image description here

EDIT 2 MWE:

\documentclass[a4paper,pdf]{article}

% \usepackage{bera}
\usepackage{listings}
\usepackage{xcolor}

\colorlet{punct}{red!60!black}
\definecolor{background}{HTML}{EEEEEE}
\definecolor{delim}{RGB}{20,105,176}
\colorlet{numb}{magenta!60!black}

\lstdefinelanguage{json}{
    basicstyle=\normalfont\ttfamily,
    numbers=left,
    numberstyle=\scriptsize,
    stepnumber=1,
    numbersep=8pt,
    showstringspaces=false,
    breaklines=true,
    frame=lines,
    backgroundcolor=\color{background},
    literate=
     *{0}{{{\color{numb}0}}}{1}
      {1}{{{\color{numb}1}}}{1}
      {2}{{{\color{numb}2}}}{1}
      {3}{{{\color{numb}3}}}{1}
      {4}{{{\color{numb}4}}}{1}
      {5}{{{\color{numb}5}}}{1}
      {6}{{{\color{numb}6}}}{1}
      {7}{{{\color{numb}7}}}{1}
      {8}{{{\color{numb}8}}}{1}
      {9}{{{\color{numb}9}}}{1}
      {:}{{{\color{punct}{:}}}}{1}
      {,}{{{\color{punct}{,}}}}{1}
      {\{}{{{\color{delim}{\{}}}}{1}
      {\}}{{{\color{delim}{\}}}}}{1}
      {[}{{{\color{delim}{[}}}}{1}
      {]}{{{\color{delim}{]}}}}{1},
}

\begin{document}
\begin{figure}[htbp]
    \centering
    \begin{lstlisting}[language=json]
    {
      "type": "request-action",
      "content": {
        "id": 2,
        "time": 1556636930397,
        "deadline": 1556636934400,
        "step": 27,
        "percept": {
          ...
        }
      }
    }
    \end{lstlisting}
    \caption{Example of a REQUEST-ACTION message.}
    \label{fig:request-action}
\end{figure}
\end{document}
PEREZje
  • 133
  • 5
  • You might try \usepackage{beramono}, which sets only \ttfamily, possibly \usepackage[scaled=0.9]{beramono} if you want it slightly bigger or smaller. You could also use DejaVu Sans Mono, which is another fork of Bitstream Vera Mono. – Davislor Jun 07 '20 at 18:26
  • Beramono seems to be different than bera. I show the difference in my updated question. – PEREZje Jun 07 '20 at 19:40
  • You don’t give a MWE for me to test, but all the bera package does is set the encoding to T1, load beraserif, berasans and beramono, and load textcomp. Since it was written in 2004, you no longer need textcomp; it’s now in the LaTeX kernel. – Davislor Jun 07 '20 at 20:07
  • 1
    Since you select \ttfamily, it should be sufficient to \usepackage{beramono} and \usepackage[T1]{fontenc}, but you might be using a bit of \berasans too. – Davislor Jun 07 '20 at 20:08
  • Alternatively, load fontspec, \setsansfont{DejaVu Sans}, and \setmonofont{DejaVu Sans Mono}. – Davislor Jun 07 '20 at 20:09
  • Sorry about that. I have amended my question with an MWE. – PEREZje Jun 07 '20 at 20:19
  • I just added the MWE, but your second answer helped out. If I added \usepackage[T1]{fontenc} it seems to have the proper font. It still feels like quite a struggle to do something as simple as change font, but I'm a LaTex newb I think so thanks! – PEREZje Jun 07 '20 at 20:20

2 Answers2

1

It also works without fontenc if you use the \usepackage{bera}. Then you have the bera serif and bera mono included:

name                       type              encoding         emb sub uni object ID
-------------------------- ----------------- ---------------- --- --- --- ---------
ZLEWRA+BeraSerif-Roman     Type 1            Custom           yes yes no       4  0
GTZSIU+BeraSansMono-Roman  Type 1            Custom           yes yes no       5  0
user187802
  • 16,850
  • If you look at update 2, using \usepackage{bera} it also changes the rest of the text in my document. Which is not what I want, or do you mean something else? – PEREZje Jun 08 '20 at 13:52
  • Then I misunderstood your problem and, of course, a simple \usepackage{beramono} will use only the mono font and leave the other families as is. – user187802 Jun 09 '20 at 06:51
1

Like this:

\documentclass[a4paper,pdf]{article}
\usepackage[T1]{fontenc}
\RequirePackage[scaled]{beramono}
\usepackage{listings}
\usepackage{xcolor}

\colorlet{punct}{red!60!black} \definecolor{background}{HTML}{EEEEEE} \definecolor{delim}{RGB}{20,105,176} \colorlet{numb}{magenta!60!black}

\lstdefinelanguage{json}{ basicstyle=\normalfont\ttfamily, numbers=left, numberstyle=\scriptsize, stepnumber=1, numbersep=8pt, showstringspaces=false, breaklines=true, frame=lines, backgroundcolor=\color{background}, literate= *{0}{{{\color{numb}0}}}{1} {1}{{{\color{numb}1}}}{1} {2}{{{\color{numb}2}}}{1} {3}{{{\color{numb}3}}}{1} {4}{{{\color{numb}4}}}{1} {5}{{{\color{numb}5}}}{1} {6}{{{\color{numb}6}}}{1} {7}{{{\color{numb}7}}}{1} {8}{{{\color{numb}8}}}{1} {9}{{{\color{numb}9}}}{1} {:}{{{\color{punct}{:}}}}{1} {,}{{{\color{punct}{,}}}}{1} {{}{{{\color{delim}{{}}}}{1} {}}{{{\color{delim}{}}}}}{1} {[}{{{\color{delim}{[}}}}{1} {]}{{{\color{delim}{]}}}}{1}, }

\begin{document} \begin{figure}[htbp] \centering \begin{lstlisting}[language=json] { "type": "request-action", "content": { "id": 2, "time": 1556636930397, "deadline": 1556636934400, "step": 27, "percept": { ... } } } \end{lstlisting} \caption{Example of a REQUEST-ACTION message.} \label{fig:request-action} \end{figure} \end{document}

This copies the lines from bera.sty that set the monospace fonts, but not the other fonts. In particular, you want the T1, not the OT1, version of the font here.

If you can use a modern engine, you might do this instead:

\documentclass[a4paper,pdf]{article}
\usepackage{listings}
\usepackage{xcolor}
\usepackage{fontspec}

\setmonofont{DejaVu Sans Mono}[Scale=MatchUppercase, Ligatures=Discretionary]

\colorlet{punct}{red!60!black} \definecolor{background}{HTML}{EEEEEE} \definecolor{delim}{RGB}{20,105,176} \colorlet{numb}{magenta!60!black}

\lstdefinelanguage{json}{ basicstyle=\normalfont\ttfamily, numbers=left, numberstyle=\scriptsize, stepnumber=1, numbersep=8pt, showstringspaces=false, breaklines=true, frame=lines, backgroundcolor=\color{background}, literate= *{0}{{{\color{numb}0}}}{1} {1}{{{\color{numb}1}}}{1} {2}{{{\color{numb}2}}}{1} {3}{{{\color{numb}3}}}{1} {4}{{{\color{numb}4}}}{1} {5}{{{\color{numb}5}}}{1} {6}{{{\color{numb}6}}}{1} {7}{{{\color{numb}7}}}{1} {8}{{{\color{numb}8}}}{1} {9}{{{\color{numb}9}}}{1} {:}{{{\color{punct}{:}}}}{1} {,}{{{\color{punct}{,}}}}{1} {{}{{{\color{delim}{{}}}}{1} {}}{{{\color{delim}{}}}}}{1} {[}{{{\color{delim}{[}}}}{1} {]}{{{\color{delim}{]}}}}{1}, }

\begin{document} \begin{figure}[htbp] \centering \begin{lstlisting}[language=json] { "type": "request-action", "content": { "id": 2, "time": 1556636930397, "deadline": 1556636934400, "step": 27, "percept": { ... } } } \end{lstlisting} \caption{Example of a REQUEST-ACTION message.} \label{fig:request-action} \end{figure} \end{document}

DejaVu Sans Mono sample

This uses more modern extensions of the same fonts.

Davislor
  • 44,045