# Change width of \lstlisting resulting from user defined \lstdefinelanguage

I used the \lstdefinelanguage to create a formatting for some JSON blocks I'd like to add to my file, I used the one in the answer for this question:

How to improve listings display of JSON files?

So I created my own \lstdefinelanguage like so:

\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},
}


And used it like this:

\begin{figure}[htbp]
\centering
\begin{lstlisting}[language=json][firstnumber=1]
{
"type": "auth-request",
"content": {
"user": "agentA1",
"pw": "1"
}
}
\end{lstlisting}
\caption{Example of an AUTH-REQUEST message.}
\label{fig:auth-request}
\end{figure}


This gives the following document:

So for my question, what is the best way to change the width of this listing? I'd like to make it something like 80% of the \textwidth if possible. But if I do this:

\begin{lstlisting}[language=json][firstnumber=1, linewidth=.8\textwidth]
{
"type": "auth-request",
"content": {
"user": "agentA1",
"pw": "1"
}
}
\end{lstlisting}


I get a warning, namely: Package Listings Warning: Text dropped after begin of listing. and it doesn't actually change the look of the listing in the document.

So how do I change the width of a specific listing?

• Does it work if you combine the two pairs of brackets into one optional argument, i.e. \begin{lstlisting}[language=json,firstnumber=1, linewidth=.8\textwidth]? Jun 7 '20 at 15:01
• It appears to be tricky to centre the listings box. It didn't work for me with figure. See tex.stackexchange.com/q/5818/35864 Jun 7 '20 at 15:08
• It does indeed lower the width, but centering it is tricky. I tried the solution from the question you sent but that seems to move the entire listing to the right instead of centering it. Jun 7 '20 at 15:15
• @moewe use tcolorbox and its listings library. It is easy there to change the width and to enlarge e.g. the left bounding box or to center. Jun 7 '20 at 15:32