how write this in verbatim latex - latex

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?

\\end{verbatim}
Should do it by making the initial \ into a literal backslash.

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.)

Use the fancyvrb package instead of the default verbatim package.

Craig's answer is good. Alternatively, \expandafter\string\csname end{verbatim}\endcsname.
These solutions won't linebreak nicely: fixing this is fiddly.

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.

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}

Related

Latex \newcommand problem

can someone please tell what I'm doing wrong here?
\newcommand{\bc}{\small\begin{verbatim}}
\newcommand{\ec}{\normalsize\end{verbatim}}
and then
\bc
1 3 6 7 89 10
22 7 7 45
\ec
but I get
Runaway argument?
^^M1 3 6 7 89 10^^M 22 7 7 45^^M\ec^^M^^M\section{Reading on\ETC.
! File ended while scanning use of \#xverbatim.
<inserted text>
\par
<*> i4c.tex
?
! Emergency stop.
<inserted text>
\par
<*> i4c.tex
I thought it was pretty safe to do that, since most commands are just text substitutions... any hints?
How \begin{verbatim} works. briefly and roughly.
\begin{verbatim} is expanded to \verbatim.
Then \verbatim sets category code of each special characters to 12.
Now all chars is like digits or puncts.
Then \verbatim sets font, parindent and calls \#xverbatim.
\#xverbatim catches the end of verbatim using the following trick:
\def\#xverbatim#1\end{#1\end}
Then \end{verbatim} finishes work.
How newcommand{\bc}{\small\begin{verbatim}} works.
\bс expands to \small\begin{verbatim}.
Then \begin{verbatim} expands to \varbatim.
\varbatim changes all categories and font.
Then \verbatim calls \#xverbatim.
\#xverbatim tries to catch your argument using the following trick:
\def\#xverbatim#1\end{#1\end}
but it is impossible because of \#xverbatim tries to catch
\end where all letters (\,e,n,d) have the category 12 and 11.
But in fact there are only \ec exsits.
\#xverbatim is trying and trying to find \end where backslash (\) has category 12 but.... File ended while scanning use of \#xverbatim
verbatim is special, it scans for a literal \end{verbatim}, as any macro substitutions are not executed after the \begin{verbatim}.
Some environment, such as verbatim, need to scan in the text ahead to find their end manually. So unlike “normal” environments, verbatim needs to find the text \end{verbatim} in the input text, verbatim.
In your example, it doesn’t (since the input text contains \ec instead.
As a workaround, you can instead use the fancyvrb package that defines a Verbatim package and allows definition custom verbatim environments.

Quick way to make 26 macros (one for each letter)

Instead of making a macro for each letter, as in
\def\bA{\mathbf{A}}
...
\def\bZ{\mathbf{Z}}
Is there a way to loop over a character class (like capital letters) and generate macros for each? I'd also like to do the same for Greek letters (using bm instead of mathbf).
\def\mydefb#1{\expandafter\def\csname b#1\endcsname{\mathbf{#1}}}
\def\mydefallb#1{\ifx#1\mydefallb\else\mydefb#1\expandafter\mydefallb\fi}
\mydefallb ABCDEFGHIJKLMNOPQRSTUVWXYZ\mydefallb
New for Greek
\def\mydefgreek#1{\expandafter\def\csname b#1\endcsname{\text{\boldmath$\mathbf{\csname #1\endcsname}$}}}
\def\mydefallgreek#1{\ifx\mydefallgreek#1\else\mydefgreek{#1}%
\lowercase{\mydefgreek{#1}}\expandafter\mydefallgreek\fi}
\mydefallgreek {beta}{Gamma}{Delta}{epsilon}{etaex}{Theta}{Iota}{Lambda}{kappa}{mu}{nu}{Xi}{Pi}{rho}\mydefallgreek
$\bGamma\bDelta \bTheta \bLambda \bXi \bPi $
$\bbeta \bgamma\bdelta \bepsilon \betaex \btheta \biota \blambda \bkappa \bmu \bnu \bxi \bpi \brho$
Expanding on Andrew's answer, here is a solution without \expandafter:
\makeatletter
\#tempcnta=\#ne
\def\#nameedef#1{\expandafter\edef\csname #1\endcsname}
\loop\ifnum\#tempcnta<27
\#nameedef{b\#Alph\#tempcnta}{\noexpand\mathbb{\#Alph\#tempcnta}}
\advance\#tempcnta\#ne
\repeat
This will define \bA, \bB, and so on, to expand to \mathbb{A}, \mathbb{B}, and so on.
Wouldn't be better to define one command
\newcommand\bm[1]{\ensuremath{${\boldmath$#1$}}$}
and it can be used both in text mode and math mode.
Usage:
\[\bm{F(x)}=\int\bm\delta(x)\ dx]
\where \mb F is blah blah blah and \bm \delta is halb halb halb...
Result:
F(x)='inegral delta(x)'dx
Where F is blah blah blah and 'delta' is halb halb halb...
Outer dollars are there to leave math (roman) mode because \boldmath command has no effect in math mode. Inner ones switch back to math (bold). Additional braces (${\boldmath) ensures that \boldmath command will work only with #1
Another advantage of this code is testing new commands for existence of \bb and \bg. So you can't crash LaTeX makros easily.
I hope this is what you're looking for.
I would recommend doing:
\newcommand{\b}[1]{\mathbf{#1}}
as Crowley says, and similar for all the other alphabets. However, if you really want to do it using LaTeX code, here's one that seems to work:
\documentclass{article}
\usepackage{amssymb}
\newcounter{char}
\setcounter{char}{1}
\loop\ifnum\value{char}<27
\edef\c{\Alph{char}}
\expandafter\expandafter\expandafter\expandafter\expandafter\expandafter\expandafter\def\expandafter\expandafter\expandafter\csname\expandafter\expandafter\expandafter b\expandafter\c\expandafter\endcsname\expandafter{\expandafter\mathbb\expandafter{\c}}
\addtocounter{char}{1}
\repeat
\begin{document}
\(\bZ\)
\end{document}
I lost count of how many 'expandafter's there are in that! To get lowercase letters, replace the Alph by alph.

How to write programming code containing the ' character in latex?

I am trying to write programming code in latex using the verbatim environment, but when I write
\begin{verbatim}
char ch = 'x';
\end{verbatim}
then the ' -characters around x are displayed incorrectly (they look "curly"). How can I fix this problem?
Load the upquote package to fix this issue in verbatim mode.
If you want straight quotes in monospaced text mode (e.g., \texttt{...}), or indeed in any other font, then you can use the \textquotesingle command defined in the textcomp package:
\documentclass{article}
\usepackage{upquote,textcomp}
\begin{document}
\newcommand\upquote[1]{\textquotesingle#1\textquotesingle}
\verb|'c'| \texttt{\upquote{h}}
\textsf{\upquote{h}} \upquote{h}
\end{document}
This will work well for fonts in any encoding rather than depending on a specific glyph slot (such as \char13 in the default OT1 encoding).
Adding \usepackage{upquote} to my preamble was sufficient.
Perhaps older versions of LaTeX or upquote required more work.
I have
What is wrong?
New
If you want to get something like this
write
\makeatletter
\let \#sverbatim \#verbatim
\def \#verbatim {\#sverbatim \verbatimplus}
{\catcode`'=13 \gdef \verbatimplus{\catcode`'=13 \chardef '=13 }}
\makeatother
For displaying source code, you might consider using the listings package; it is quite powerful and offers an option to display “straight” quotation marks.
If you're seeing curly single right quotes in a verbatim environment, then the single right quote in your typewriter font is curly, and that's the correct one to use for what you're doing (which I assume is displaying some C code).
\textsf{``} and \textsf{''} come pretty close to straight quotes. No need for using any special packages.
This is what I got from another source, and this works.
Use `` to start the double quotes (this symbol is below ~ symbol on our keyboard)
Use '' to close the double quotes (this symbol is below the " symbol on our keyboard)
So, `` quote double, unquote double''
Same goes for single quotes, `quote single, unquote single'

LaTeX, tildes and verbatim mode

Following on from this question, I'm totally stumped on getting LaTeX to give me a tilde when I'm in verbatim mode. It has to be a tilde because it's the type of a function!
sig symm : (Board, [(Int, Int)]) ~> Bool
Standard methods for displaying a tilde are printed verbatim, of course..
Any suggestions?
An edit to clarify: Typing a ~ in verbatim mode gives an accent above a blank space. I'm after a tilde as it appears at the beginning of this sentence.
If there are some characters that do not occur in your input, you can use fancyvrb and its commandchars option to insert TeX commands within verbatim text:
\documentclass{article}
\usepackage{fancyvrb}
\begin{document}
\newcommand{\mytilde}{$\sim$}
\begin{Verbatim}[commandchars=\\\{\}]
sig symm : (Board, [(Int, Int)]) \mytilde> Bool
\end{Verbatim}
\end{document}
See the documentation of fancyvrb for more.
Perhaps you should look at: LaTeX package to do syntax highlighting of code in various languages which has suggestions for typesetting code...
I assumed that listing would do it for you, but failing that alltt and fancyvrb are alternatives to verbatim. See this search on CTAN for other possibilities.
If you are using listing command, you can set the tilde to be literal. Likt this.
\documentclass
\usepackage{listings}
\lstset{
literate={~} {$\sim$}{1} % set tilde as a literal (no process)
}
\begin{document}
\begin{lstlisting}
~
\end{lstlisting}
\end{document}
\begin{verbatim}
~
\end{verbatim}

Using \verbatim as part of an argument for a macro?

Most things I've wanted to do in LaTeX have either been straight-forward, or easily found on the web, but this one has been stumping me.
I have a macro 'solution': to apply some common formatting to each solution:
\newcommand\solution[1]{{\\ \\* \bf Solution.}#1\qed \newpage}
Which has worked nicely so far, but now I wanted to include a drawing which I've done quickly using 'ASCII Art' so I'd like to use \verbatim. But it doesn't work, it produces the following errors:
Runaway argument?
...
! File ended while scanning use of \#xverbatim.
From what I read in the "Not So Short introduction to LaTeX", \verbatim can't be used this way. I assume there is a better way to do what I'm trying to do?
This is a FAQ.
You could try changing \newcommand to \newenvironment and then use something like
\begin{solution}
\begin{verbatim}
[ascii art here]
\end{verbatim}
\end{solution}
Use the package cprotect
e.g. ( with tcolorbox but it is working with most of the newcommand created)
\usepackage{tcolorbox}
\usepackage{cprotect}
\newcommand{\tcb}[1] {
\begin{tcolorbox} [arc=0mm,colback=bginf,coltitle=black!70!black,colframe=black!30!white,width=\linewidth,fontupper=\bfseries\small,halign title=flush center,halign upper=center]
#1
\end{tcolorbox}
}
\cprotect\tcb{\scriptsize
\begin{verbatim}
...
\end{verbatim}
}

Resources