Latex \newcommand problem - latex

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.

Related

Code in latex for braces

In LaTeX text, I sometimes need 'unpaired' parenthesis - ( without ) or vice versa; however, in such a case, delimiter check macro reports error. I found a simple solution: \symbol{40} and \symbol{41}, which aren't recognized as delimiters. The same works for brackets [ and ] (decimal codes are 91 and 93 respectively), but doesn't work for braces { and }. Their codes are 123 and 125 respectively, but \symbol{123} and \symbol{125} produce something like dash and quote. I use fontenc LCY, trying both text and math modes.
I've found a solution:\textbraceleft and \textbraceright, but why \symbol doesn't work in such a case?
Found it! This first code
\documentclass[border=5mm]{standalone}
\begin{document}
\symbol{123} and \symbol{125}
\end{document}
gives
as output, while
\documentclass[border=5mm]{standalone}
\usepackage[T1]{fontenc}
\begin{document}
\symbol{123} and \symbol{125}
\end{document}
gives this output instead:
Adding
\usepackage[T1]{fontenc}
provides the desired font encoding (fontenc package).
A (not-so-further) reading suggestion: font encoding vs. input encoding.

user defined commands in verbatim environment

is it possible to create a new command with a parameter in a verbatim environment and some indentation?
\newcommand{codeblock}[1]{\begin{quote}\begin{verbatim}#1\end{verbatim}\end{quote}}
This does not work. Is there an other way?
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{\codeblock}[1]{\begin{quote}\begin{verbatim}#1\end{verbatim}\end{quote}} works.
First of all \codeblock{Some {}$&%^_} reads its argument.
#1 --> Some code {}$&%^_
Note: {,},$,&,%,^,_ have categories 1,2,3,4,6,7,8 rather than 11 or 12!!!)
\codeblock expands to \begin{quote}\begin{verbatim} Some {}$&%^_\end {verbatim}\end {quote}.
Important: backslash of \end has category 0 rather than 11.
Moreover { and } have categories 1 and 2 rather than 11.
And $,&,%,^,_ have categories 3,4,6,7,8.
\begin{quote} expands to \quote and \quote executes.
Then \begin{verbatim} expands to \varbatim.
\varbatim changes all categories and font. But (important)
the category of backslash (in \end) remains equal to 0. And the categories of {, }, $, &, %, ^, _ typed after Some remains because of "argument reading" executes before \verbatim changes all categories. But you need that all char has categories 11 o 12.
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 four letters with other category code:
\ with category 0 and e,n,d with category 11.
It is possible (I am not sure) that trick is more masterly:
Red chars have category 12. Green chars have category 11.
\def, \#xverbatim, \end are macros with \ (category 0) and letters (category 11).
\#xverbatim is trying and trying to find \end where backslash (\) has category 11 but.... File ended while scanning use of \#xverbatim
If you want to create new macro \codeblock you must do something like above text.
It looks like you want to have code in your document, in which case you're probably better served by the package listings than by verbatim. Listings also gives you nice features like line numbering and syntax highlighting for many common languages. See http://www.ctan.org/tex-archive/macros/latex/contrib/listings/ if it's not already installed with your LaTeX distribution.

Latex \newcommand for \end{verbatim} et.al not working

I'm trying to make Latex usable, by introducing some timesavers, but I'm having trouble with defining new commands that terminate environments, completely at random.
This works:
\newcommand{\bcv}{\ensuremath{\begin{smallmatrix}}}
\newcommand{\ecv}{\ensuremath{\end{smallmatrix}}}
\newcommand{\be}{\begin{enumerate}}
\newcommand{\ee}{\end{enumerate}}
This does not work:
\newcommand{\bal}{\begin{align*}}
\newcommand{\eal}{\end{align*}}
\newcommand{\verbass}[1]{\begin{verbatim} #1 \end {verbatim}}
Specifically, I think the \end value is just ignored?
When I try to use \verbass{Halp} I get an error: !File ended while scanning use of \#xverbatim.
Obviously I can use \begin{foo} ... \end{foo} at all locations as needed, but really, this should work!
How \begin{verbatim} works. briefly and roughly.
\begin{verbatim} is expanded to \verbatim.
Then \verbatim sets category code of each characters to 11.
Now all chars is letters.
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{\verbass}[1]{\begin{verbatim} #1 \end {verbatim}} work.
First of all \verbass{Halp} reads its argument.
#1 --> Halp
\verbass expands to \begin{verbatim} Halp \end {verbatim}.
Important: backslash of \end has category 0 rather than 11.
Moreover { and } have categories 1 and 2 rather than 11.
Then \begin{verbatim} expands to \varbatim.
\varbatim changes all categories and font. But (important)
the category of backslash (in \end) remains equal to 0.
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 11.
But in fact there are four letters with other category code:
\ with category 0 and e,n,d with category 11.
\#xverbatim is trying and trying to find \end where backslash (\) has category 11 but.... File ended while scanning use of \#xverbatim
There's a very brief explanation here about verb and verbatim. Basically, LaTeX insists verb and verbatim get the first "look" at their contents.
I am far away from being a professional programmer, but a quick-and-dirty-solutions could be:
\newcommand{\myverbatim}[1]{\begin{tt} \detokenize{#1} \end{tt} \\ }
The output is not very nice, because linebreaks are ignored. If this is desired one could repeat the command line-by-line (?).

how write this in verbatim 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}

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'

Resources