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.
Post Reply
DryRun
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?

Post by DryRun »

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

\begin{listing}[H]
    \begin{minted}
    [
    frame=lines,
    framesep=2mm,
    baselinestretch=1.2,
    linenos
    ]
    {cpp}
    void angle_PD()
    {
      anglePWM = kp_angle * (angleSetpoint - angle) + kd_angle * (angularSpeed0 - angular_speed);
    }
    \end{minted}
    \caption{PD controller}
    %\label{code:3}
    \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
pdcontroller.png (13.18 KiB) Viewed 4721 times

Recommended reading 2024:

LaTeXguide.org • LaTeX-Cookbook.net • TikZ.org

NEW: TikZ book now 40% off at Amazon.com for a short time.

And: Currently, Packt sells ebooks for $4.99 each if you buy 5 of their over 1000 ebooks. If you choose only a single one, $9.99. How about combining 3 LaTeX books with Python, gnuplot, mathplotlib, Matlab, ChatGPT or other AI books? Epub and PDF. Bundle (3 books, add more for higher discount): https://packt.link/MDH5p

kaiserkarl13
Posts: 707
Joined: Tue Mar 25, 2008 5:02 pm

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

Post by kaiserkarl13 »

You want "breaklines=true" inside lstset.
DryRun
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?

Post by DryRun »

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

baselinestretch=1.2,
linenos
breaklines=true
But it gives an error.
User avatar
Ijon Tichy
Posts: 640
Joined: Mon Dec 24, 2018 10:12 am

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

Post by Ijon Tichy »

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.
Sorry, but I can no longer participate here as the administrator is trampling on my wishes on one of his other platforms. :cry:
Post Reply