我为一些我想添加到文档中的 JSON 块创建了一个 \lstdefinelanguage 命令,我遵循了以下问题的答案:
在答案中,回答者对语言的定义如下:
\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},
}
回答者使用 bera 字体,这使得 JSON 看起来更具视觉吸引力。我想复制这个。唯一的问题是,当我添加命令时,\usepackage{bera}
整个文档中的所有文本都会更改为此字体,而这并不是我想要的。我只希望在使用 JSON 语言创建列表时使用 bera 字体。最好的方法是什么?
更新:
当我按照@Davislor的建议尝试时\usepackage{beramono}
,我得到了以下输出:
我想要的输出看起来像这样,当我使用时我得到它\usepackage{bera}
:
编辑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}
答案1
像这样:
\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}
这会复制该等宽字体集中的行bera.sty
,但不会复制其他字体。具体来说,您需要的是 T1 版字体,而不是 OT1 版字体。
如果你可以使用现代引擎,你可以这样做:
\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}
这使用了相同字体的更多现代扩展。
答案2
fontenc
如果您使用 ,它也可以不使用\usepackage{bera}
。然后您就拥有了包含 bera serif 和 bera mono 的内容:
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