Generalc++ source code

LaTeX specific issues not fitting into one of the other forums of this category.
Post Reply
maceta
Posts: 1
Joined: Sat Jul 21, 2007 11:53 pm

c++ source code

Post by maceta »

Hi, what's the best way to insert c++ source code into a latex document? I'm using texlive 2007...

Thanks for any advice.

Recommended reading 2024:

LaTeXguide.org • LaTeX-Cookbook.net • TikZ.org
LaTeX Beginner's Guide LaTeX Cookbook LaTeX TikZ graphics TikZによるLaTeXグラフィックス
User avatar
localghost
Site Moderator
Posts: 9201
Joined: Fri Feb 02, 2007 12:06 pm

c++ source code

Post by localghost »

Try the listings package. Its documentation describes in detail everything you need.
How to make a "Minimal Example"
Board Rules
Avoidable Mistakes[/size]

¹ System: openSUSE 42.2 (Linux 4.4.52), TeX Live 2016 (vanilla), TeXworks 0.6.1
olivares14031
Posts: 10
Joined: Sun Jan 14, 2007 6:48 pm

Re: c++ source code

Post by olivares14031 »

You can use verbatim environment

\begin{verbatim}
C++ Code to go HERE

/* the first C++ program
* - just outputs `Hello, World!'
*/

#include <iostream> // declarations for I/O

int main() // main function main()
{
/* print `Hello, World!' on standard output channel std::cout
* followed by an endline (std::endl)
*/
std::cout << "Hello, World!" << std::endl;
}
\end{verbatim}

It should do the job.
User avatar
localghost
Site Moderator
Posts: 9201
Joined: Fri Feb 02, 2007 12:06 pm

c++ source code

Post by localghost »

olivares14031 wrote: You can use verbatim environment […] It should do the job.

What about the often demanded syntax highlighting and other formatting matters?
How to make a "Minimal Example"
Board Rules
Avoidable Mistakes[/size]

¹ System: openSUSE 42.2 (Linux 4.4.52), TeX Live 2016 (vanilla), TeXworks 0.6.1
rockstar1707
Posts: 27
Joined: Wed Jan 17, 2007 12:35 pm

c++ source code

Post by rockstar1707 »

localghost wrote:
olivares14031 wrote: You can use verbatim environment […] It should do the job.

What about the often demanded syntax highlighting and other formatting matters?


One possible workaround for preserving syntax highlightninh is to open source file with Scite text editor (http://scintilla.sourceforge.net/SciTE.html) and to export it as LaTeX file. It produces a really nice .tex file with all the "usual" formating.
User avatar
localghost
Site Moderator
Posts: 9201
Joined: Fri Feb 02, 2007 12:06 pm

c++ source code

Post by localghost »

rockstar1707 wrote: One possible workaround for preserving syntax highlightninh is to open source file with Scite text editor (http://scintilla.sourceforge.net/SciTE.html) and to export it as LaTeX file. It produces a really nice .tex file with all the "usual" formating.

A workaround is completely unnecessary. The listings package is the best way to include source code into LaTeX documents. It offers settings for nearly every programming language.
How to make a "Minimal Example"
Board Rules
Avoidable Mistakes[/size]

¹ System: openSUSE 42.2 (Linux 4.4.52), TeX Live 2016 (vanilla), TeXworks 0.6.1
yateleybob
Posts: 9
Joined: Mon Jul 09, 2007 1:46 pm

Re: c++ source code

Post by yateleybob »

The listings package is definitely the way to go. Syntax highlighting is built-in but customisable.
Post Reply