7

I am writing a package which uses the verbatim package.

\RequirePackage{verbatim}

For the documentation I'm using the ltxdoc class. So I have to put an % character before every line of the documentation. Also when I'm using the vebatim environment to typeset code examples like

% \begin{verbatim}
% Some Code
% Some other code
% \end{verbatim}

ltxdoc removes all the % at beginning of each line, also inside the verbatim environment.

It seems that the verbatim package redefines the special verbatim environment from the ltxdoc class, because when I write

% \begin{verbatim}
% some code example
% some other code example
% \end{verbatim}

the % before Some code and before Some other code is still there in the produced pdf. So the pdf-output is

% some code example
% some other code example

But it should be

some code example
some other code example

Can anyone help me to solve this problem?

Minimal working example (MWE)

This is an MWE of the .dtx file.

% \iffalse
%<package>\NeedsTeXFormat{LaTeX2e}[1999/12/01]
%<package>\ProvidesPackage{exercises}[2014/04/22 v1.0 .dtx exercises file]
%
%<*driver>
\documentclass[a4paper]{ltxdoc}
\usepackage{exercises}
\begin{document}
  \DocInput{exercises.dtx}
\end{document}
%</driver>
% \fi
% \GetFileInfo{exercises.sty}
% \title{The \textsf{exercises} package\thanks{This document
%   corresponds to \textsf{exercises}~\fileversion, dated \filedate.}}
% \author{Roger Jud}
%
% \maketitle
%
% \begin{verbatim}
% \begin{exercise}
%   Calculate $1 + 1 =$
% \end{exercise}
% \end{verbatim}
%
% \StopEventually{\PrintChanges\
%
%    \begin{macrocode}
  \RequirePackage{verbatim}
  \newenvironment{exercises}{Start}{End}
%    \end{macrocode}
% \Finale
\endinput
  • Please post an MWE of your dtx file, so that people can test what goes wrong with their own eyes. – jubobs Apr 22 '14 at 16:19
7

AFAIK environment verbatim does not support the start of a line with a percent character as in the DTX file format. The following workaround can be done:

  1. The environment verbatim is written without the percent prefix:

    \begin{verbatim}
    \begin{exercise}
      Calculate $1 + 1 =$
    \end{exercise}
    \end{verbatim}
    
  2. The inclusion in the files, generated by docstrip can be avoided by using an unused guard:

    %<*verb>
    \begin{verbatim}
    \begin{exercise}
      Calculate $1 + 1 =$
    \end{exercise}
    \end{verbatim}
    %</verb>
    
  3. However, the guard disturbs the setting as documentation. This can be fixed by a meta comment, the same kind that is also used to exclude the driver part from the documentation:

    % \iffalse
    %<*verb>
    % \fi
    \begin{verbatim}
    \begin{exercise}
      Calculate $1 + 1 =$
    \end{exercise}
    \end{verbatim}
    % \iffalse
    %</verb>
    % \fi
    

Complete example:

% \iffalse
%<package>\NeedsTeXFormat{LaTeX2e}[1999/12/01]
%<package>\ProvidesPackage{exercises}[2014/04/22 v1.0 .dtx exercises file]
%
%<*driver>
\documentclass[a4paper]{ltxdoc}
\usepackage{exercises}
\begin{document}
  \DocInput{exercises.dtx}
\end{document}
%</driver>
% \fi
% \GetFileInfo{exercises.sty}
% \title{The \textsf{exercises} package\thanks{This document
%   corresponds to \textsf{exercises}~\fileversion, dated \filedate.}}
% \author{Roger Jud}
%
% \maketitle
%
% \iffalse
%<*verb>
% \fi
\begin{verbatim}
\begin{exercise}
  Calculate $1 + 1 =$
\end{exercise}
\end{verbatim}
% \iffalse
%</verb>
% \fi
%
% \StopEventually{\PrintChanges}
%
%    \begin{macrocode}
  \RequirePackage{verbatim}
  \newenvironment{exercises}{Start}{End}
%    \end{macrocode}
% \Finale
\endinput

Result

Package listings

If you consider to use package listings instead, then you can keep the percent prefixes, because listings can gobble characters from the start of line. The following example uses the verbatim like settings of egreg's answer for question "How can I make lstlisting look exactly like verbatim?" and adds gobble=2:

% \iffalse
%<package>\NeedsTeXFormat{LaTeX2e}[1999/12/01]
%<package>\ProvidesPackage{exercises}[2014/04/22 v1.0 .dtx exercises file]
%
%<*driver>
\documentclass[a4paper]{ltxdoc}
\usepackage{exercises}
\usepackage{listings}
\lstnewenvironment{dtxverblisting}{%
  \lstset{
    gobble=2,
    basicstyle=\ttfamily,
    columns=fullflexible,
    keepspaces=true,
  }%
}{}
\begin{document}
  \DocInput{exercises.dtx}
\end{document}
%</driver>
% \fi
% \GetFileInfo{exercises.sty}
% \title{The \textsf{exercises} package\thanks{This document
%   corresponds to \textsf{exercises}~\fileversion, dated \filedate.}}
% \author{Roger Jud}
%
% \maketitle
%
% \begin{dtxverblisting}
% \begin{exercise}
%   Calculate $1 + 1 =$
% \end{exercise}
% \end{dtxverblisting}
%
% \StopEventually{\PrintChanges}
%
%    \begin{macrocode}
  \RequirePackage{verbatim}
  \newenvironment{exercises}{Start}{End}
%    \end{macrocode}
% \Finale
\endinput

Result

  • You were faster. ;-) – egreg Apr 22 '14 at 19:37

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.