Math & ScienceFormat source code in latex

Information and discussion about LaTeX's math and science related features (e.g. formulas, graphs).
Post Reply
tonguim
Posts: 24
Joined: Fri Nov 06, 2009 4:32 pm

Format source code in latex

Post by tonguim »

Hi all,

please,
1-how can i format my source code to get exactly the following format in my pdf file
2-how can do to have all the following code on one page (i use beamer):

Code: Select all

\begin{verbatim}
	main ()
	{ int pid;
		switch (pid = fork()) 
		{
		  case –1:
		     /* fork échoue */
		     perror(''appel fork a echoue'');
		     exit(1);
		  case 0:
		     /* valeur retournée au processus-fils */
		     printf(''Fils: PID = %d\n'', getpid());
		     break;
		default:
		     /* processus père */
		    printf(''Pere: PID fils = %d\n'', pid);
		break;
		}
		} /* end main */
\end{verbatim}
As you can see, i use the verbatim environement but it doesn't works for this code; in the pdf document, all the text is align at the right margin.

I use the verbatim environment for smaller text, in the same document, and it works fine.

Thank you.
Last edited by tonguim on Mon Nov 30, 2009 5:44 pm, edited 1 time in total.

Recommended reading 2024:

LaTeXguide.org • LaTeX-Cookbook.net • TikZ.org
LaTeX books
mas
Posts: 226
Joined: Thu Dec 04, 2008 4:39 am

Format source code in latex

Post by mas »

Take a look at the listings package. I have just made rough draft for your code below :

Code: Select all

\documentclass{article}
%
\usepackage{listings}

\begin{document}
\lstset{language=C}
\begin{lstlisting}
main ()
   { int pid;
      switch (pid = fork())
      {
        case –1:
           /* fork échoue */
           perror(''appel fork a echoue'');
           exit(1);
        case 0:
           /* valeur retournée au processus-fils */
           printf(''Fils: PID = %d\n'', getpid());
           break;
      default:
           /* processus père */
          printf(''Pere: PID fils = %d\n'', pid);
      break;
      }
   } /* end main */
\end{lstlisting}

\end{document}
Regards,
Last edited by mas on Mon Nov 30, 2009 6:58 pm, edited 1 time in total.

OS: Debian/GNU Linux; LaTeX System : TeXLive; Editor : Vim
tonguim
Posts: 24
Joined: Fri Nov 06, 2009 4:32 pm

Re: Format source code in latex

Post by tonguim »

Hi Mas,

thank you for answer. But the lstlisting environment doesn't produces any effect on my code.

Regards
User avatar
localghost
Site Moderator
Posts: 9202
Joined: Fri Feb 02, 2007 12:06 pm

Format source code in latex

Post by localghost »

tonguim wrote:[…] But the lstlisting environment doesn't produces any effect on my code. […]
Define »effect on the code« and explain what you are aiming at. I think the listings manual describes very good how to format source code in a document.


Best regards
Thorsten¹
Last edited by localghost on Wed Dec 02, 2009 6:30 pm, edited 1 time in total.
josephwright
Site Moderator
Posts: 814
Joined: Tue Jul 01, 2008 2:19 pm

Format source code in latex

Post by josephwright »

Indeed, for me

Code: Select all

\documentclass{beamer}
\begin{document}
\begin{frame}[fragile]
\begin{verbatim}
   main ()
   { int pid;
      switch (pid = fork())
      {
        case –1:
           /* fork échoue */
           perror(''appel fork a echoue'');
           exit(1);
        case 0:
           /* valeur retournée au processus-fils */
           printf(''Fils: PID = %d\n'', getpid());
           break;
      default:
           /* processus père */
          printf(''Pere: PID fils = %d\n'', pid);
      break;
      }
      } /* end main */
\end{verbatim}
\end{frame}
\end{document}
seems to be fine.
Last edited by josephwright on Wed Dec 02, 2009 6:38 pm, edited 1 time in total.
Joseph Wright
holzensp
Posts: 6
Joined: Thu Dec 03, 2009 2:57 pm

Format source code in latex

Post by holzensp »

Tonguim said he was using beamer. I'm guessing the problem is not in the lstlisting environment, but rather how beamer deals with verbatim-type environments. This is why mas' code probably doesn't compare.

Tonguim, did you remember to make the slide fragile (as Joseph did)?

Code: Select all

\begin{frame}[fragile]
\frametitle{Your title}

\begin{lstlisting}
code
\end{lstlisting}
\end{frame}
Post Reply