Hi, what's the best way to insert c++ source code into a latex document? I'm using texlive 2007...
Thanks for any advice.
General ⇒ c++ source code
NEW: TikZ book now 40% off at Amazon.com for a short time.
- localghost
- Site Moderator
- Posts: 9202
- Joined: Fri Feb 02, 2007 12:06 pm
c++ source code
Try the listings package. Its documentation describes in detail everything you need.
How to make a "Minimal Example"
Board Rules
Avoidable Mistakes
¹ System: TeX Live 2025 (vanilla), TeXworks 0.6.10
Board Rules
Avoidable Mistakes
¹ System: TeX Live 2025 (vanilla), TeXworks 0.6.10
-
- Posts: 10
- Joined: Sun Jan 14, 2007 6:48 pm
Re: c++ source code
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.
\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.
- localghost
- Site Moderator
- Posts: 9202
- Joined: Fri Feb 02, 2007 12:06 pm
c++ source code
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
¹ System: TeX Live 2025 (vanilla), TeXworks 0.6.10
Board Rules
Avoidable Mistakes
¹ System: TeX Live 2025 (vanilla), TeXworks 0.6.10
-
- Posts: 27
- Joined: Wed Jan 17, 2007 12:35 pm
c++ source code
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.
- localghost
- Site Moderator
- Posts: 9202
- Joined: Fri Feb 02, 2007 12:06 pm
c++ source code
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
¹ System: TeX Live 2025 (vanilla), TeXworks 0.6.10
Board Rules
Avoidable Mistakes
¹ System: TeX Live 2025 (vanilla), TeXworks 0.6.10
-
- Posts: 9
- Joined: Mon Jul 09, 2007 1:46 pm
Re: c++ source code
The listings package is definitely the way to go. Syntax highlighting is built-in but customisable.