Recently, because of the need to present C + + code in latex, we searched the web for solutions provided by others. The result was disappointing, the latex code that was searched for was either not running, or the result was not in line with its own needs. Domestic use of Baidu to search, foreign use of Yahoo to search. The most hateful is some paste out of latex code to explain the less, and the preamble (preamble) missing the corresponding package, wasting the user's time. Originally want to use others directly, but did not achieve the desired effect. This stimulated me, I took the time to go through the latex to show the code mechanism, and finally understand the mechanism, now explained below.
1 Pre-Introduction packages added
Presentation code in Latex needs to use the listings package, because the code to use a different color font display more beautiful, but also need to xcolor package. Packages in Latex are like header files in the C + + language, which can only be provided by introducing their corresponding functions. Finally, the previous introduction 1 should be written like this:
\usepackage{listings}\usepackage[usenames,dvipsnames]{xcolor}
2 listings package corresponding parameter setting
We don't do any setup and we get the results, but the results are often monotonous, not the result we want. Before we can really use it, we have to make the appropriate settings to achieve the effect we need. This is where the real cost is. After several debugs, my settings are:
\definecolor{Mygreen}{RGB}{0,0.6,0}\definecolor{Mygray}{RGB}{0.5,0.5,0.5}\definecolor{Mymauve}{RGB}{0.58,0,0.82}\lstset{Backgroundcolor=\color{lightgray}, Basicstyle = \footnotesize, Breakatwhitespace = False, Breaklines = true, Captionpos = B, Commentstyle = \color{Mygreen}\bfseries, Extendedchars = False, frame =shadowbox, framerule=0.5PT, Keepspaces=true, Keywordstyle=\color{Blue}\bfseries,% keyword styleLanguage = C + +,% The language of codeotherkeywords={string}, Numbers=left, numbersep=5PT, Numberstyle=\tiny\color{Mygray}, Rulecolor=\color{Black}, Showspaces=false, Showstringspaces=false, Showtabs=false, stepnumber=1, Stringstyle=\color{Mymauve},% string literal styleTabsize=2, title=\lstname}
In the above settings, in order to better use the color, we defined three custom colors and then used them in the listings settings. Most of the above setting options are as the name implies, I explain the more important of the meaning of several options, others please refer to the corresponding information.
- Basicstyle the format used to set the code font
- Captionpos used to set where the code block header appears
- Commentstyle the font format used to set comments in code
- Language is used to specify what language the code is in, this example uses C + +
- Numbers is used to specify where the line number of a code block appears or does not appear
Note: The above setup code should also appear in the previous introduction of Latex.
3 Latex shows C + + code and running results
In the body of the Latex code, attribute the following code:
\begin{lstlisting}[caption={}] #include " RandomGenerator.h " void normalnumgen (double mean, double SD, int num, string filename) { const int nrolls=num; //number of eperiments default_random_engine generator;normal_distribution<< Span class= "Hljs-keyword" >double > Dnorm (mean, SD); Ofstream outfile (Filename,ios:: out ); for ( int i= 0 ; i < nrolls; ++i) { double number = Dnorm (generator); outfile << number << Endl;}} \end{lstlisting}
Above is the specific way to use the listings package, you need to include the full C + + code in the environment lstlisting. The results of the final operation are as follows:
4 concluding remarks
This package is very fun and more detailed introduction please refer to the Listings package user manual.
- The so-called pre-introduction, is the majority of latex textbooks, foreign or domestic, defined, in the \documentclass below, \begin{document} above that part of the area.
Using Latex to show C + + code