0

I want to add reference to my c++ project, but it doesn't seem quite right. The code doesn't has any alignment, and it get cropped with the paper. It look like this:

how it looks

I use this to link my code to latex:

    \lstset{language=C++,
        numberstyle=\ttfamily,
        basicstyle=\ttfamily,
        keywordstyle=\color{blue}\ttfamily,
        stringstyle=\color{red}\ttfamily,
        commentstyle=\color{gray}\ttfamily,
        morecomment=[l][\color{magenta}]{\#}
    }

    \lstinputlisting[label={foo},caption={\textit{Source code} program}, language={C++}]{main.cpp}

How to add alignment to the code?

1
  • I think you are looking for the breaklines option, which be default is false. The documentation of the listings package provides further options to control these automatic line breaks. Sep 28, 2021 at 6:20

1 Answer 1

1

What about combining the following 2 improvements?

(i) as pointed out by Andrew Swann: wrap lines using the breaklines option, which be default is false. ("The documentation of the listings package provides further options to control these automatic line breaks")

(ii) wrap the whole listing inside a \small environment (or even \tiny), which may make it easier to read and visually nicer

PS This is posted as an Answer because I'm a newbie and don't yet have privileges to comment, otherwise I'd make it a comment!

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.