LaTeX forum ⇒ Graphics, Figures & TablesHow to split long C/C++ code line on two lines without inserting new line number?

Information and discussion about graphics, figures & tables in LaTeX documents.
Posts: 8
Joined: Sun May 19, 2019 7:08 pm

How to split long C/C++ code line on two lines without inserting new line number?

Postby DryRun » Sun Oct 13, 2019 12:20 am

I am using the following snippet to insert C/C++ code into my LaTeX document but the problem is that the code on one line is too long and it goes over the right side of the document page. How to split this on two lines? I tried to simply move part of the line to another line but then it also introduces a new line number on the side. I would like to keep the same line number but i'm not sure how to do that.

Code: [Select all] [Expand/Collapse] [Download] ({untitled.tex})
  1. \begin{listing}[H]
  2. \begin{minted}
  3. [
  4. frame=lines,
  5. framesep=2mm,
  6. baselinestretch=1.2,
  7. linenos
  8. ]
  9. {cpp}
  10. void angle_PD()
  11. {
  12. anglePWM = kp_angle * (angleSetpoint - angle) + kd_angle * (angularSpeed0 - angular_speed);
  13. }
  14. \end{minted}
  15. \caption{PD controller}
  16. %\label{code:3}
  17. \end{listing}

I tried to split the single long line into two lines but the problem as shown in the screenshot below is that the next line now has a different line number (4) and i would like to keep them both on a single line (3).
pdcontroller.png (13.18 KiB) Viewed 103 times

Posts: 636
Joined: Tue Mar 25, 2008 5:02 pm

Postby kaiserkarl13 » Sun Oct 13, 2019 7:50 am

You want "breaklines=true" inside lstset.

Posts: 8
Joined: Sun May 19, 2019 7:08 pm

Postby DryRun » Sun Oct 13, 2019 9:02 pm

kaiserkarl13 wrote:You want "breaklines=true" inside lstset.

I'm not sure where to find lstset. I looked inside macros.tex and preamble.tex but i couldn't find it.

I also tried adding
Code: [Select all] [Expand/Collapse] [Download] ({untitled.tex})
  1. baselinestretch=1.2,
  2. linenos
  3. breaklines=true

But it gives an error.

User avatar
Ijon Tichy
Posts: 120
Joined: Mon Dec 24, 2018 10:12 am

Postby Ijon Tichy » Mon Oct 14, 2019 9:59 am

The comma after option linenos is missing. For more information about options like breaklines see section “5.3 Available options” in the manual of package minted.

BTW: \lstset is a listings command. For minted you can setup the options either with \setminted or as optional argument of the minted environment. See the manual for more information.

BTW: If you have a problem, instead of a screenshot and a code snippet a Infominimal working example that makes it possible to reproduce this problem and to test a suggestions would be useful. And error messages should be quoted. Something like “gives an error” is far away from being enough information. It is more like, “my dishwasher does not wash”. Not enough information, neither to describe the symptom nor for a proper remote diagnostic.

Return to “Graphics, Figures & Tables”

Who is online

Users browsing this forum: No registered users and 10 guests