Text FormattingProper Formatting for C Code

Information and discussion about LaTeX's general text formatting features (e.g. bold, italic, enumerations, ...)
Post Reply
smilypauleta
Posts: 2
Joined: Thu Jan 10, 2013 5:19 pm

Proper Formatting for C Code

Post by smilypauleta »

Hello everyone,

I need to type C code into LaTeX and send it electronically to my lecturer. I already know the verbatim and listings package, but they do not work for my purpose. (or I do not know how to use them properly)

The thing is that my lecturer will copy-paste my code and test it on his C compiler. When I use verbatim or listings, I have two major problems:
  1. When my code is on one long line, it goes beyond the page. (for example printf(...) can be longer than one line and yet I cannot "cut" it)
  2. When I copy paste some code from the PDF onto my own compiler, it changes the structure of the code (for example it adds spaces between letters, changes the ", etc.)
Therefore my lecturer will not be able to compile correctly the code.

Does anyone have an idea on how to solve these problems ?

Thank you very much !

Recommended reading 2024:

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

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

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

Proper Formatting for C Code

Post by localghost »

smilypauleta wrote:[…]When my code is on one long line, it goes beyond the page. (for example printf(...) can be longer than one line and yet I cannot "cut" it) […]
Option breaklines should do.
smilypauleta wrote:[…] When I copy paste some code from the PDF onto my own compiler, it changes the structure of the code (for example it adds spaces between letters, changes the ", etc.)
Option columns=fullflexible should do.

For details refer to the listings manual. Think about using minted alternatively.


Best regards and welcome to the board
Thorsten
smilypauleta
Posts: 2
Joined: Thu Jan 10, 2013 5:19 pm

Re: Proper Formatting for C Code

Post by smilypauleta »

Thank you very much for your quick reply.

The breaklines does the job, so this problem is solved. However, when I copy paste the pdf onto my C compiler, it transforms brackets { and } into letters f and g respectively...so it is not completely solved yet. In any case I am going to inspect the minted package.

Thank you for your help !
Post Reply