GeneralHow do I type the code(c++ like) in latex and keep the space

LaTeX specific issues not fitting into one of the other forums of this category.
Post Reply
akira32
Posts: 45
Joined: Tue Nov 13, 2007 10:32 am

How do I type the code(c++ like) in latex and keep the space

Post by akira32 »

How do I type the code(c++ like) in latex and keep the space?
I want to copy the shader code to latex, but it seems to be diffcult to modify.
How do I keep the format of my shader code and paste in latex?

My shader code as below: (a part of my complete shader code)

Code: Select all

Ps_DEFAULT_OUT PsRenderIntoShadowDepthBuffer(Vs_DEFAULT_OUT IN)
{
	Ps_DEFAULT_OUT OUT=(Ps_DEFAULT_OUT)0;
	
#ifdef _USE_NL_
	OUT.color.xyz = tex2D(tex_col_diffuse, IN.v2_tc.xy).xyz * saturate(dot(normalize(v3_light_dir), normalize(IN.v3_normal)));
#else
	OUT.color.xyz = tex2D(tex_col_diffuse, IN.v2_tc.xy).xyz;//*GetDiffuseLighting(IN.v4_pos_view, IN.v3_normal);
#endif
	
	#ifdef _TEST_3_13_
	OUT.color.w = IN.v4_pos_view.z/IN.v4_pos_view.w;
	#else
	OUT.color.w = IN.sdb_z;
	#endif
	
	return OUT;	
}
Sorry! I get the answer.

Code: Select all

\scriptsize
 
\begin{verbatim}
.... the code
\end{verbatim}

Recommended reading 2024:

LaTeXguide.org • LaTeX-Cookbook.net • TikZ.org

NEW: TikZ book now 40% off at Amazon.com for a short time.

User avatar
Stefan Kottwitz
Site Admin
Posts: 10321
Joined: Mon Mar 10, 2008 9:44 pm

How do I type the code(c++ like) in latex and keep the space

Post by Stefan Kottwitz »

Hi,

the listings package provides a lot of features for typesetting code listings, it knows the C++ language, it can emphasize identifiers or keywords, change the style or color of comments, frame a listing by a box, even with color, provides a list of listings and much more.
Just a small example without many features:

Code: Select all

\documentclass[a4paper,10pt]{article}
\usepackage{listings}
\begin{document}

\lstset{language=C++,basicstyle=\footnotesize}
\begin{lstlisting}[caption=Test]
Ps_DEFAULT_OUT PsRenderIntoShadowDepthBuffer(Vs_DEFAULT_OUT IN)
{
   Ps_DEFAULT_OUT OUT=(Ps_DEFAULT_OUT)0;
   
#ifdef _USE_NL_
   OUT.color.xyz = tex2D(tex_col_diffuse, IN.v2_tc.xy).xyz *
      saturate(dot(normalize(v3_light_dir),
      normalize(IN.v3_normal)));
#else
   OUT.color.xyz = tex2D(tex_col_diffuse, IN.v2_tc.xy).xyz;
      //*GetDiffuseLighting(IN.v4_pos_view, IN.v3_normal);
#endif
   
   #ifdef _TEST_3_13_
   OUT.color.w = IN.v4_pos_view.z/IN.v4_pos_view.w;
   #else
   OUT.color.w = IN.sdb_z;
   #endif
   
   return OUT;   
}
\end{lstlisting}

\end{document}
The package has a very good documentation, there you will find reference and examples.

Stefan
LaTeX.org admin
Post Reply