Text Formattingignoring LaTex code

Information and discussion about LaTeX's general text formatting features (e.g. bold, italic, enumerations, ...)
Post Reply
thedreamshaper
Posts: 44
Joined: Wed Aug 04, 2010 7:00 pm

ignoring LaTex code

Post by thedreamshaper »

I am writting a short introduction to the program "R", and i need to display some code, however some of it will also make LaTex do stuff, so i need it to ignore this part. How do i do this :) ?


ex.

Code: Select all

datanavn <- read.table('C:/Documents and Settings/Thor/Desktop/puzzle.txt',header=T)

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

frabjous
Posts: 2064
Joined: Fri Mar 06, 2009 12:20 am

ignoring LaTex code

Post by frabjous »

You probably want to use a verbatim enviroment, or better, a package like listings designed for source code.
thedreamshaper
Posts: 44
Joined: Wed Aug 04, 2010 7:00 pm

ignoring LaTex code

Post by thedreamshaper »

I am having a litlle trouble with the \verb command

Code: Select all

\verb=datanavn <- read.table('C:/Documents and Settings/Thor/Desktop/puzzle.txt',header=T)=
This will not show the "=T" bit :(

Any ideas :) ?
User avatar
frabjous
Posts: 2064
Joined: Fri Mar 06, 2009 12:20 am

ignoring LaTex code

Post by frabjous »

The first character that comes after \verb is the delimiter. It is used to mark where the scope of the command starts and stops. You need to choose a character that doesn't occur in what comes after, or it will be misinterpreted as the end of the scope. So you can't use = if what you're using including has an = in it. So in this case, you could use | instead:

Code: Select all

\verb|datanavn <- read.table('C:/Documents and Settings/Thor/Desktop/puzzle.txt',header=T)|
For something this long, however, consider using an environment:

Code: Select all

\begin{verbatim}
datanavn <- read.table('C:/Documents and Settings/Thor/Desktop/puzzle.txt',header=T)
\end{verbatim}
Or an appropriate package like listings, as I suggested above. It even has a language setting specifically for R.
thedreamshaper
Posts: 44
Joined: Wed Aug 04, 2010 7:00 pm

ignoring LaTex code

Post by thedreamshaper »

Great it works :)

for bonus point meaby you could tell me how i make the text fit inside by box ?

Code: Select all

\begin{tabularx}{\linewidth}{|p{3.5cm}|p{6cm}|p{6cm}|}
\hline
Anvendelse & Kode & kommentar\\
\hline
Indlæs table til R: & 
\begin{verbatim} datanavn <- read.table('C:/Documents and Settings/Thor/Desktop/puzzle.txt',header=T) \end{verbatim} &

Et table gemmes fra stien(med omvendt skrå streger) i "datanavn", filen der indlæses har overskrifter for variablene og header sættes derfor til \verb="T" (true)=3\\
\hline%\bottomrule
\end{tabularx}
User avatar
frabjous
Posts: 2064
Joined: Fri Mar 06, 2009 12:20 am

Re: ignoring LaTex code

Post by frabjous »

Do you want it to wrap?

Otherwise, you'll have to rethink your layout. There's no way you can squeeze that much text into that small an area. You'd have to shrink it down to the point of illegibility.
thedreamshaper
Posts: 44
Joined: Wed Aug 04, 2010 7:00 pm

Re: ignoring LaTex code

Post by thedreamshaper »

wrap would be what i am looking for, how do i achieve this :) ?
User avatar
frabjous
Posts: 2064
Joined: Fri Mar 06, 2009 12:20 am

ignoring LaTex code

Post by frabjous »

Well, you can't use LaTeX's automatic wrapping facilities with a verbatim environment. You could instead use the appropriate commands (requires the textcomp package) that do the equivalent of the signs that "do things", \textless for < and so on. Even then, since there are so many characters in a row here without a single space, you'll need to shrink the font size down:

Code: Select all

\scriptsize\texttt{datanavn \textless- read.table(\textquotesingle C:/Documents and Settings/Thor/Desktop/puzzle.txt\textquotesingle ,header=T)}
If you wanted to stick with verbatim-type commands you could do the breaking manually, using a separate \verb command for each line:

Code: Select all

\verb|datanavn <-| \newline
\verb|read.table('C:/Documents and | \newline
\verb|Settings/Thor/Desktop/| \newline
\verb|puzzle.txt',header=T)|
Both of those are very very ugly. I strongly urge you to rethink your layout. You could have a nice code box (see the listings package for some nice options) with a label on the top, and comment underneath. A series of these would be preferable to a big table where everything is squeezed.
Post Reply