Text FormattingRedefine 'verbatim' Environment

Information and discussion about LaTeX's general text formatting features (e.g. bold, italic, enumerations, ...)
Post Reply
eleanor
Posts: 19
Joined: Sat Oct 03, 2009 6:03 pm

Redefine 'verbatim' Environment

Post by eleanor »

Hi. I've got a problem redefining my verbatim environment.

Here is a use case:

Code: Select all

% preamble
\documentclass[11pt,a4paper]{article}
\pagestyle{plain}
\setlength{\textwidth}{17cm}
\setlength{\oddsidemargin}{-1cm}
\setlength{\evensidemargin}{-2cm}

% included packages
\usepackage{verbatim,listings,fancyvrb}
\usepackage{color}
\usepackage{url}
\usepackage{tipa}
\usepackage[utf8]{inputenc}
\usepackage{graphicx,epsfig}
\usepackage{parskip}
\usepackage{ifthen}
\usepackage{caption}
\usepackage{hevea}



%
% Define new command 'verbatim" which is used to hold the code of any language.
%
\let\oldverbatim=\verbatim
\let\oldendverbatim=\endverbatim
\renewenvironment{verbatim}[1][test]
{
  \oldverbatim
}
{ 
  \oldendverbatim
}


\begin{document}

  This is the example command:
  \begin{verbatim}
# ls -l
  \end{verbatim}

\end{document}
This is not the whole code, so don't ask: why do you have such meaningless code in there - in the original article, it means something - because I also have other code defined.


The error when compiling with pdflatex is:

Code: Select all

! Illegal parameter number in definition of \next.
<to be read again>
                   }
l.43 #
       ls -l
The problem is that the redefinition of verbatim environment scans for the optional argument in the newline - towards the '#' character ... and it fails.

If I set the following:

Code: Select all

\catcode`\#=11\relax
Then the problem goes away and the compilation completes successfully.

My question now is: how to enable the verbatim package to read the argument correctly without needing to define the catcodes. Is it possible to set the verbatim environment like that - so it only reads for optional arguments in the current line, and if no arguments given, then there is no arguments present. What's in the next line is already the verbatim text?

Recommended reading 2024:

LaTeXguide.org • LaTeX-Cookbook.net • TikZ.org

NEW: TikZ book now 40% off at Amazon.com for a short time.

User avatar
localghost
Site Moderator
Posts: 9202
Joined: Fri Feb 02, 2007 12:06 pm

Redefine 'verbatim' Environment

Post by localghost »

eleanor wrote:[…] This is not the whole code, so don't ask: why do you have such meaningless code in there - in the original article, it means something - because I also have other code defined. […]
It seems that you did not understand the meaning of a minimal example. In case you use the term "language" here in terms of a programming language, you should take a look at one of the packages listings or minted.


Thorsten
eleanor
Posts: 19
Joined: Sat Oct 03, 2009 6:03 pm

Re: Redefine 'verbatim' Environment

Post by eleanor »

Minimal example is example where you present the problem you're having in as little code as possible, so others can help you figure out the problem.

About your reply: I don't want to use listings or minted, I want to use my redefined verbatim - but it doesn't work now - because of the problem I outlined.

So I would only like little help solving the problem I'm having. I don't want to use another package.
eleanor
Posts: 19
Joined: Sat Oct 03, 2009 6:03 pm

Re: Redefine 'verbatim' Environment

Post by eleanor »

So, can anybody help me?
User avatar
Stefan Kottwitz
Site Admin
Posts: 10321
Joined: Mon Mar 10, 2008 9:44 pm

Redefine 'verbatim' Environment

Post by Stefan Kottwitz »

Hi Eleanor,
eleanor wrote:I don't want to use listings or minted
you already load listings, as I can see in the example code.

Stefan
LaTeX.org admin
User avatar
Stefan Kottwitz
Site Admin
Posts: 10321
Joined: Mon Mar 10, 2008 9:44 pm

Redefine 'verbatim' Environment

Post by Stefan Kottwitz »

Hi Eleanor,

you could quote the # symbol: \#

Minimal example:

Code: Select all

\documentclass{article}
\usepackage{verbatim}
\let\oldverbatim=\verbatim
\let\oldendverbatim=\endverbatim
\renewenvironment{verbatim}[1][test]
{
  \oldverbatim
}
{
  \oldendverbatim
}
\begin{document}
  This is the example command:
  \begin{verbatim}
\# ls -l
  \end{verbatim}
\end{document}
This is how a minimal example could look like. All the settings regarding margins, color, graphics etc. are not important for the problem and could be omitted, making it easier for readers and testers.

Stefan
LaTeX.org admin
eleanor
Posts: 19
Joined: Sat Oct 03, 2009 6:03 pm

Re: Redefine 'verbatim' Environment

Post by eleanor »

Hello. Let me just say that I'm grateful for the answer, but this is not what I want. I would only like to change the definition of verbatim - not the actual code inside it.

Maybe I can define a command like this:
\verbset[a=first_arg, b=second_arg]

And use that inside the verbatim environment - so the verbatim wouldn't have any arguments at all.

How can I do that?
Post Reply