Text Formattingproblem with verbatim layout

Information and discussion about LaTeX's general text formatting features (e.g. bold, italic, enumerations, ...)
Post Reply
verbal-l
Posts: 3
Joined: Mon Aug 10, 2009 9:06 pm

problem with verbatim layout

Post by verbal-l »

Hi,

I am using
\begin{verbatim}

to embedd some code into my document - however the formatting gets lost, I would like to retain the spaces and indentation of the text - I am aware of the * option to show whitepsaces but this is not what I want. Is there a way to display the code with the same formatting as i.e. in Visual Studio without having to add the spaces by hand inside Latex? When I copy/paste the Visual Studio code into the document it is displayed correctly, however when rendered everything is right-aligned.

Any tricks? Thanks.

Recommended reading 2024:

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

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

And: Currently, Packt sells ebooks for $4.99 each if you buy 5 of their over 1000 ebooks. If you choose only a single one, $9.99. How about combining 3 LaTeX books with Python, gnuplot, mathplotlib, Matlab, ChatGPT or other AI books? Epub and PDF. Bundle (3 books, add more for higher discount): https://packt.link/MDH5p

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

problem with verbatim layout

Post by localghost »

Use the listings package to format the code. Usable solutions can be found by using the search function of the forum.


Best regards
Thorsten
carol
Posts: 105
Joined: Wed Dec 24, 2008 7:25 pm

Re: problem with verbatim layout

Post by carol »

I also used listing package and according to the user's guide, it has an automatic feature of breaking long codes but it doesn't.

I just want to use one line code that doesn't worth using listing but since it's long, it goes beyond the page margin. How is it possible to display the one-line code in page layout using \begin{verbatim} ... or \verb+....+

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

problem with verbatim layout

Post by localghost »

carol wrote:I also used listing package and according to the user's guide, it has an automatic feature of breaking long codes but it doesn't.[…]
You have to set the line break feature explicitly.

Code: Select all

\lstset{%
  breaklines=true
}
Post Reply