[Tex/LaTex] Tabular environment within the \newenvironment

environmentstables

I'm writing a book, in which we refer the reader to the CD-ROM for the source code, and tell him the compiler and the result of running the program. Here's an example:

expected

Since this "template" is happening a lot in the book, I decided to define a new environment for it (the command \cprotEnv used below is from the package cprotect):

\newenvironment{CDROM}[2]
    {\begin{tabular}{| m{0.5in} m{0.3in} m{5in} |}
        \hline
        \includegraphics[width=0.5in]{CD-ROM.pdf} && #1 \\
        & \includegraphics[width=0.3in]{Hand.pdf} &
        We used the free compiler #2. \\
        & \includegraphics[width=0.3in]{Hand.pdf} &
    }
    {\\ \hline \end{tabular}}

\cprotEnv\begin{CDROM}{\verb"\SampleCodes\C++\GetPageSize"}{Open Watcom}
The page size for this system is 4096 bytes.
\end{CDROM}

However, the output is something peculiar:

produced

Could you please help me to get the expected results?

Best Answer

The problem is that \cprotEnv is thought for particular environments such as align that don't accept verbatim in their body. If your schemes are always like the example, with two "handed lines", it's easier to define a command rather than an environment:

\newcommand{\CDROM}[3]
    {\begin{tabular}{| m{0.5in} m{0.3in} m{5in} |}
        \hline
        \includegraphics[width=0.5in]{CD-ROM.pdf} && #1 \\
        & \includegraphics[width=0.3in]{Hand.pdf} &
        We used the free compiler #2. \\
        & \includegraphics[width=0.3in]{Hand.pdf} & #3\\ \hline \end{tabular}}

and then use it as

\cprotect[mmm]\CDROM{\verb"\SampleCodes\C++\GetPageSize"}{Open Watcom}
  {The page size for this system is 4096 bytes.}

A way that frees you from \cprotect is

\newcommand{\CDROM}{\begingroup\catcode`\#=12 \xCDROM}
\newcommand{\xCDROM}[3]
    {\begin{tabular}{| m{0.5in} m{0.3in} m{5in} |}
        \hline
    \includegraphics[width=0.5in]{CD-ROM.pdf} &&
          \ttfamily\catcode`\\=12 \catcode`\ =9
          \scantokens{#1} \\
        & \includegraphics[width=0.3in]{Hand.pdf} &
        We used the free compiler #2. \\
        & \includegraphics[width=0.3in]{Hand.pdf} & #3\\ \hline \end{tabular}\endgroup}

and now

\CDROM{\SampleCodes\C++\GetPageSize}{Open Watcom}
  {The page size for this system is 4096 bytes.}
\CDROM{\SampleCodes\C#\GetPageSize}{}{}

work without \verb. This, however, assumes that you don't need spaces in the "verbatim" first argument.

By splitting the command into two parts we can ensure that the # character is not doubled as it would happen if we added \catcode`\#=12 along with the other settings.

Related Question