I think the alltt
environment from the package with the same name is better suited here. It makes everything verbatim except \
,{
and }
, similar to
\begin{Verbatim}[commandchars=\\\{\}]
.
The mentioned added space is due to the used \fboxsep
which makes the box bigger. You can avoid this by setting this value to zero, either globally or locally using a own macro. This however makes the box very tight on all four sites. An added \strut
makes sure that it has always the same height and depth.
\documentclass{article}
\usepackage{xcolor}
\usepackage{alltt}
% Global:
\setlength{\fboxsep}{0pt}
% Better:
\newcommand\hi[2][red]{{%
\setlength{\fboxsep}{0pt}%
\colorbox{#1}{#2\strut}%
}}
\begin{document}
\begin{alltt}
hello \colorbox{red}{world\strut} blah
hello world blub
hello \hi{world} blub
\end{alltt}
\end{document}
Another alternative, the best one IHMO, is to compensate for the fboxsep
before and after the box:
\documentclass{standalone}
\usepackage{xcolor}
\usepackage{alltt}
% Compensate for fbox sep:
\newcommand\Hi[2][red]{%
\hspace*{-\fboxsep}%
\colorbox{#1}{#2}%
\hspace*{-\fboxsep}%
}
\begin{document}
\begin{alltt}
hello world blah
hello \Hi{world} blah
\end{alltt}
\end{document}
Result:
The main problem with using your definition of myverbatim
can be defined in terms of a combination of scope and macro replacement. That is, something that starts with
\begin{<some-env>}
is expected to end with
\end{<some-env>}
If it doesn't, there's a problem, since the scope is not clearly defined. In your case, you start it with
\begin{myverbatim}
which is internally replaced with (removing the font change to \tiny
for now)
\begin{verbatim}
Now, since you're in the verbatim
environment (that is, a successful call was made to \begin{verbatim}
), LaTeX starts gobbling up contents and printing them as-is until it arrives at a single line of code that resembles
\end{verbatim}
That's just how the verbatim
environment works. It it looking for the stopping point. However, this never happens since the replacement of \end{myverbatim}
(in your code) to \end{verbatim}
(as per your definition) never occurs within the verbatim
environment; verbatim
doesn't perform the replacement of macros with their definitions. As such, TeX just keeps on scanning, producing what is considered a "Runaway argument" error.
Taking the above into account, a broad view on the replacement leaves your MWE to resemble (I've added a comment):
\documentclass{article}
\newenvironment{myverbatim}{\tiny\begin{verbatim}}{\end{verbatim}}
\begin{document}
\tiny\begin{verbatim}% Replacement text for \begin{myverbatim}
test
\end{myverbatim}% No replacement since you're in the verbatim environment
\end{document}
Clearly the above doesn't start and end with the same environment.
If you want to have more flexibility with verbatim
, use the verbatim
package which allows you to do the following:
\documentclass{article}
\usepackage{verbatim}% http://ctan.org/pkg/verbatim
\newenvironment{myverbatim}%
{\endgraf\tiny\verbatim}%
{\endverbatim}
\begin{document}
Some text before.
\begin{myverbatim}
test
test
\end{myverbatim}
Some text after.
\end{document}
The verbatim
package is a means to obtain your desired solution, just like fancyvrb
and listings
would be. Of course, a more rudimentary implementation (specific to your case) would be to merely change the font used by verbatim
- stored in the macro \verbatim@font
:
\makeatletter
\renewcommand{\verbatim@font}{\tiny\ttfamily}
\makeatother
Sure you can just precede the verbatim
environment with \tiny
and it will work just as well, but \verbatim@font
is called within the verbatim
environment, and therefore is constrained to only function there, not outside of it.
Best Answer
Here is an option with
fancyvrb
where you can change the formatting of specific lines; the code should be self-explanatory:It should work for elementary setups and small code formatting.