Formatting code in LaTex is as astonishingly easy, once one knows how to do it.
There is a command “tabbing”, which is used in the same way as for example “centering”, except that one has to tell the computer where to set a tab.
\begin{tabbing}
if \= (condition) \{ \\ % inserts a tab just after the "if"-command.
\> then statement \=\\ % go to the defined tab and set a new one
\}\\
else \{ \\
\> else statement \> next tab is here\\
\}\\
\end{tabbing}
Copy-paste the code into your LaTex-file and see yourself.
One can use as many \> in a sequence as needed, as long as enough tabs have been defined before.
E.g.
\begin{tabbing}
o \= o \= o\\
o \> \> o\\
o \> o \> o\\
\end{tabbing}
Another way to just format your code if you already have it in a “tabbed” version is to use “verbatim”.
\begin{verbatim}
Your code goes here.
\end{verbatim}
March 15th, 2010 at 2:40 am
Thanks very much! Been searching for how write C++ code as is in a latex file awhile (i guess searching for “latex” and “code” was never likely to turn up the right pages…)
Kudos
July 13th, 2011 at 3:50 pm
For the last line of the second last example shouldn’t you have used:
o \> o \> o\\… or am I misunderstanding something?
July 14th, 2011 at 6:04 am
You are right, thanks! Tom.
August 24th, 2011 at 7:43 am
Thanks for this. Linking to something like http://kb.mit.edu/confluence/display/ist/How+to+use+Tabs+in+LaTeX might clarify matters a little bit.
August 6th, 2012 at 4:43 pm
the page has moved to:
http://kb.mit.edu/confluence/display/istcontrib/How+to+use+Tabs+in+LaTeX
August 6th, 2012 at 4:50 pm
Thanks for the update! Regards, Tom.
November 2nd, 2012 at 10:36 am
nice thankyou
April 7th, 2013 at 9:08 am
Thank you very much simple example as this, was exactly what I was looking for..:)