how write this in verbatim latex
Asked Answered
A

6

8

i have a little question about the verbatim environment in latex..... my problem is how can i write "\end{verbatim}" inside of a verbatim environment, it supposed that everything inside a verbatim is written just like it looks but this is the string to end the environment, so how can i do this?

Aframe answered 11/6, 2010 at 2:9 Comment(0)
E
4

I think it might be impossible in a verbatim environment -- at least, that's what Lamport says in the LaTeX book (first paragraph on pg. 64). However, you can do it with the \verb command: \verb+\end{verbatim}+ works just fine (and in fact, you can use any character instead of + as long as you make them match).

That said, the listings package is smart enough to have an escape character. For example,

\begin{lstlisting}[escapechar=+]
+\verb!\end{lstlisting}!+
\end{lstlisting}

does exactly what you're looking for. (Again, pick a different character than + if you're already using that for something else.)

Evetteevey answered 11/6, 2010 at 2:48 Comment(2)
thanks, that works. Mmm I need a little more help, in a listing environment do you know how can i do the same? (write \end{lstlisting})I first ask for verbatim, because both environments write the text inside like they are written....the first solution that comes to my mind is just simply split the text in two parts so in the middle write \end{lstlisting} inside a \verbAframe
Oh, lstlisting is smart enough to have an escape character for that -- updating answer now.Evetteevey
M
5
 \\end{verbatim}

Should do it by making the initial \ into a literal backslash.

Mcduffie answered 11/6, 2010 at 2:44 Comment(0)
E
4

I think it might be impossible in a verbatim environment -- at least, that's what Lamport says in the LaTeX book (first paragraph on pg. 64). However, you can do it with the \verb command: \verb+\end{verbatim}+ works just fine (and in fact, you can use any character instead of + as long as you make them match).

That said, the listings package is smart enough to have an escape character. For example,

\begin{lstlisting}[escapechar=+]
+\verb!\end{lstlisting}!+
\end{lstlisting}

does exactly what you're looking for. (Again, pick a different character than + if you're already using that for something else.)

Evetteevey answered 11/6, 2010 at 2:48 Comment(2)
thanks, that works. Mmm I need a little more help, in a listing environment do you know how can i do the same? (write \end{lstlisting})I first ask for verbatim, because both environments write the text inside like they are written....the first solution that comes to my mind is just simply split the text in two parts so in the middle write \end{lstlisting} inside a \verbAframe
Oh, lstlisting is smart enough to have an escape character for that -- updating answer now.Evetteevey
H
2

Use the fancyvrb package instead of the default verbatim package.

Hackathorn answered 11/6, 2010 at 2:46 Comment(2)
can you give and example?.....i read the documentation but i can't see the answerAframe
Did you go to the link, download the fancyvrb package and use the Verbatim environment (capital V?) If so, what did you see?Hackathorn
B
2

I have a simpler solution. Write a non-breaking space, in Emacs it's C-x 8 SPC (Windows, though I haven't tried it is Alt+0160 (on numeric keypad)). So, you can do this:

\begin{verbatim}
\_end{verbatim}
\end{verbatim}

where the first \end{verbatim} has this "_", the non-breaking character.

Barquentine answered 12/6, 2010 at 16:57 Comment(2)
it adds a ugly space where it should notAframe
no, it doesn't. It will add that character in the source, but it will solve your problem.Barquentine
Z
1

Craig's answer is good. Alternatively, \expandafter\string\csname end{verbatim}\endcsname.

These solutions won't linebreak nicely: fixing this is fiddly.

Zoometry answered 11/6, 2010 at 5:19 Comment(0)
B
0

Here is my method, thanks @S.Lott for his advice.

If you want to write \end{verbatim} in the verbatim environment, you have to using another 'verbatim' layer to nest your code (this is where fancyvr package comes to play). Here it is:

\documentclass{article}
\usepackage{fancyvrb}           
\begin{document}
            
Some text here
    
\begin{Verbatim} % note 'uppercase V' in verbatim
                
\begin{verbatim}
Code example    
\end{verbatim}      
                
\end{Verbatim}
            
\end{document}
Baez answered 16/8, 2022 at 10:36 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.