# How to align reference code?

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:

I use this to link my code to latex:

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

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


How to add alignment to the code?

• 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

(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