Writing computer or programming code in Latex

To write a code in latex, you can use \begin{verbatim} \end{verbatim} environtment. A similar approach is by doing this


\usepackage{fancyvrb}
\DefineVerbatimEnvironment{code}{Verbatim}{fontsize=\small}
\DefineVerbatimEnvironment{example}{Verbatim}{fontsize=\small}

and then you can type your code in the body

\begin{code}
c+=1;
\end{code}

Advertisements

8 Responses

  1. Thank you, t works

  2. Thank u. T works

  3. Thanks!

  4. Another approach is provided by the listings package:

    \documentclass[a4paper,10pt]{article}
    \usepackage{listings}
    \lstset{language=C++}
    \begin{document}
    \begin{lstlisting}
    #include
    #include

    int main()
    {
    std::cout << "Hallo Welt!" << std::endl;
    }
    \end{lstlisting}
    \end{document}

    This package provides a lot of features for typesetting source code, many languages are predefined.

    Stefan

  5. Thanks for guidance, it was perfectly useful in order to solve my problem

  6. […] The busiest day of the year was March 1st with 213 views. The most popular post that day was Writing computer or programming code in Latex. […]

  7. thanks for the post! any idea why it won’t allow this code environment inside an \fbox ?

  8. […] have a latex code for a computer program code like as specified in here. I want to write a label to this code. I searched for it but was unable to find how to add label […]

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s