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}
Eldon Reeves
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 🙂
Gerry
For the last line of the second last example shouldn’t you have used:
o \> o \> o\\
… or am I misunderstanding something?
tom
You are right, thanks! Tom.
Robert Stackhouse
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.
Ton
the page has moved to:
http://kb.mit.edu/confluence/display/istcontrib/How+to+use+Tabs+in+LaTeX
tom
Thanks for the update! Regards, Tom.
noij
nice thankyou
meydlo
Thank you very much simple example as this, was exactly what I was looking for..:)
shane
Thanks for the share really helpful for everyone newbie or for experienced ones