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:


And used it like this:

      "type": "auth-request",
      "content": {
        "user": "agentA1",
        "pw": "1"
    \caption{Example of an AUTH-REQUEST message.}

This gives the following document:

enter image description here

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"

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]?
    – moewe
    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
    – moewe
    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.
    – PEREZje
    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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.