user defined commands in verbatim environment - latex

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.

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.

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

Triple-wrapping of \colorbox → \NewEnviron → \newenvironment fails

I am trying to wrap an environment created with \NewEnviron (package 'environ') into an old good \newenvironment:
\NewEnviron{test}{\colorbox[gray]{0.7}{\BODY}}
\newenvironment{wrapper}{\begin{test}}{\end{test}}
\begin{wrapper}
debug me
\end{wrapper}
However, this gives me a strange error:
LaTeX Error: \begin{test} on input line 15 ended by \end{wrapper}.
LaTeX Error: \begin{wrapper} on input line 15 ended by \end{document}.
If I replace \NewEnviron{test}{aaa(\BODY)bbb} with \newenvironment{test}{aaa(}{)bbb} — everything works as expected! It seems like \NewEnviron fails to find its end for some reason.
I'm trying to do some magic with 'floatfig' wrapped into a \colorbox so I need a way to convert \colorbox to an environment and wrap it into another one. I can define a new command but it's not a very good idea.
The thing is that \NewEviron and \newenvironment works in different ways.
1) \newenvironment{test}{aaa(}{)bbb} defines two commands: \test is aaa( and \endtest is )bbb.
\begin{test} is expanded to \test.
\end{test} is expanded to \endtest and checks that your scope begins with begin{test} rather \begin{something else}, for example \begin{wrapper}.
2) \NewEviron{test}{aaa(\BODY)bbb} defines \test in different way. First of all \test catches the \BODY using the following trick
\def\test#1\end{\def\BODY{#1}aaa(\BODY)bbb\testcontinue}
(name \testcontinue may be different) and inserts aaa(\BODY)bbb. Then \testcontinue checks that \end on some input line ended by \end{test} rather than \end{something else}. Macro \endtest
is not needed because it is never executed.
Look on your code:
\begin{wrapper}
debug me
\end{wrapper}
\begin{wrapper} is expanded to \begin{test}. Then
\begin{test} is expanded to \test. \test catch \BODY.
Attention! \BODY is equal to debug me. And now \testcontionue checks
that \end after \BODY ended by \end{test}. It is not true. \end{test} is absent.
There is \end{wrapper}.
You want to say that \end{wrapper} must be expanded to \end{test}. But \end before wrapper was eaten by
macro \test: #1\end{\def\BODY{#1}aaa(\BODY)bbb\testcontinue}
and can not be executed.
I hope I success to explain.
I've found a hacky trick to create an environment that can be wrapped in another one. One should use saveBoxes like this:
\newenvironment{example}[2][]{%
\newsavebox{\exampleStore} % Box storage
\begin{lrbox}{\exampleStore} % Start capturing the input
}{%
\end{lrbox} % Stop capturing the input
\colorbox[gray]{0.7}{%
\usebox{\NBstorage} % Load the box's contents
}%
}%
}%

Latex listings-package format option for uppercase keywords

I use the listings package to insert source code. I would like to print all keywords uppercase in the output, regardless of the case in the input.
The manual states that
keywordstyle=[number][*]style
produces just what I want. However the following (almost) minimal example does not work.
if I set keywordstyle to "[1][]{\bfseries}" I end up with "[]" in front of every keyword
and "[*]{\bfseries}" gives me an asterisk in the start of the document.
I also tried "\MakeUppercase" and "{\MakeUppercase}" for keywordstyle which resulted in several errors, the first being:
! Incomplete \iffalse; all text was ignored after line 11
Minimal example:
\documentclass{article}
\usepackage{listings}
\lstdefinelanguage{KA_assembler}
{morekeywords={add,and,or,xor},
keywordstyle=[1][*]{\bfseries},
sensitive=false,
}
\lstset{language=KA_assembler}
\begin{document}
\begin{lstlisting}
and %r1, %r2
xor %r2, %r3
and %r4, %r5
\end{lstlisting}
\end{document}
I use Miktex for compilation of the tex files. So how do I force uppercase for Keywords?
In the manual, the brackets around the * look a bit different then the brackets around number. The reason is that the brackets around * are not meant to be used in the latex code, they just indicate that the presence of the * is optional. So try
keywordstyle=[1]*\bfseries
or
keywordstyle=*\bfseries
- it worked for me.

Latex Multiple Linebreaks

I use LaTeX to type up programming homeworks for classes. I need to do this:
my line of text blah blah blah
new line of text with blank line between
I know I can use double slash to break lines \\, but LaTeX will only take the first line break (complains about more) and starts a new line, it produces this :
my line of text blah blah blah
new line of text with blank line between
How can I get that extra line break in there so I can have space between my lines of text?
Line break accepts an optional argument in brackets, a vertical length:
line 1
\\[4in]
line 2
To make this more scalable with respect to font size, you can use other lengths, such as \\[3\baselineskip], or \\[3ex].
Do you want more space between paragraphs? Then you can change the parameter \parskip.
For example, try
\setlength{\parskip}{10pt plus 1pt minus 1pt}
This means that the space between paragraphs is usually 10pt, but can grow or shrink by up to 1pt. This means you give LaTeX the ability to change it up to one 1pt in order to achieve a better page layout. You can remove the plus and minus parts to make it always your specified length.
If you are trying to display source code, try the listings package or use verbatim. If you are trying to typeset pseudocode, try the algorithm package.
Insert some vertical space
blah blah blah \\
\vspace{1cm}
to scale to the font, use ex (the height of a lowercase "x") as the unit, and there are various predefined lengths related to the line spacing available, you might be particularly interested in baselineskip.
You can use the setspace package which gives you spacing environments, e.g.:
\documentclass{article}
\usepackage{setspace}
\begin{document}
\doublespace
my line of text blah blah blah
new line of text with blank line between
\end{document}
Or use a verbatim environment to control the layout of your code precisely.
For programs you are really better off with a verbatim or alltt environment, but if you want a blank line that LaTeX will not bitch about, try
my line of text blah blah blah\\
\mbox{ }\\ %% space followed by newline
new line of text with blank line between
While verbatim might be the best choice, you can also try the commands \smallskip , \medskip or guess what, \bigskip .
Quoting from this page:
These commands can only be used after
a paragraph break (which is made by
one completely blank line or by the
command \par). These commands output
flexible or rubber space,
approximately 3pt, 6pt, and 12pt high
respectively, but these commands will
automatically compress or expand a
bit, depending on the demands of the
rest of the page
I find that when I include a blank line in my source after the \\ then I also get a blank line in my output. Example:
It's time to recognize the income tax as another horrible policy mistake like banning beer, and to return to the tax policies that were correct in the Constitution in the first place. Our future depends on it.
\\
Wherefore the 16th Amendment must forthwith be repealed.
However you are correct that LaTeX only lets you do this once. For a more general solution allowing you to make as many blank lines as you want, use \null to make empty paragraphs. Example:
It's time to recognize the income tax as another horrible policy mistake like banning beer, and to return to the tax policies that were correct in the Constitution in the first place. Our future depends on it.
\null
\null
\null
Wherefore the 16th Amendment must forthwith be repealed.
\\\\
This works on pdfLatex. It creates 2 new lines for you.
Maybe try inserting lines with only a space?
\ \\
\ \\
This just worked for me:
I was trying to leave a space in the Apple Pages new LaTeX input area. I typed the following and it left a clean line.
\mbox{\phantom{0}}\\

Resources