This is a follow-up to the question: How can I typeset a command and its literal equivalent in an environment?

It would obviously be useful to handle environments too. I imagine something like the following:


some example text

Desired Output

The output would be something like this:

enter image description here

Pseudo Code

\usepackage{booktabs} % Adds support for \toprule, \midrule, \bottomrule
\usepackage{array} % https://tex.stackexchange.com/a/4816/13552

% Some flipped out function called "environment" goes here:
% \NewEnviron is what I would normally use to capture the body in \BODY
% \NewDocumentEnvironment is what egreg used for the command solution.


\begin{environment}{mytab}{This environment is a wrapper for the standard latex table environment. This environment is a wrapper for the standard latex table environment. You can optionally adjust the alignment of columns by adding parameters. (e.g. \string$c\string^c\string^c)}
col1 & col2 & col3 \\
dat1 & dat2 & dat3 \\
dat4 & dat5 & dat6 \\


Update 27-04-2015

I've added the following code to the preamble:


  \tl_set:Nn \l_env_argument_i_tl { #1 }
  \tl_set:Nn \l_env_argument_ii_tl { #2 }
  \tl_set:Nn \l_env_argument_iii_tl { #3 }
  \l_env_argument_iii_tl \par\noindent % Description
  \tl_set_rescan:NnV \l_env_argument_tl {} \l_env_argument_i_tl
  \tl_if_blank:VF \l_env_argument_ii_tl
    \tl_set_rescan:NnV \l_env_temp_tl {} \l_env_argument_ii_tl
    \tl_put_right:Nx \l_env_argument_tl { { \exp_not:V \l_env_temp_tl } }
\tl_new:N \l_env_argument_tl
\tl_new:N \l_env_argument_i_tl
\tl_new:N \l_env_argument_ii_tl
\tl_new:N \l_env_argument_iii_tl
\tl_new:N \l_env_temp_tl
\cs_generate_variant:Nn \tl_set_rescan:Nnn { NnV }

The output should be an example of literal code and an example as it would look when typeset.

enter image description here

Problem with automatically inserting code:

%\begin{environment}{<environment>}{<any parameters>}{<description>}
\begin{environment}{tabular}{{ccc}}{This is the standard latex environment for tabular data.}
% \begin{tabular}{ccc} <-- This gets automatically inserted
col1 & col2 & col3 \\
dat1 & dat2 & dat3 \\
% \end{tabular} <-- This gets automatically inserted

Another option I was thinking about due to difficulties capturing the text inside the environment is to put the tabular data into arg3 and move the description into the body of the environment (just exchange them). If I do this, I still run into the problem that I need to add \begin{ + arg1 + } + arg2 + arg3 + \end{ + arg1 + }. I am not sure how to do this.

  • 2
    Did you try out the solutions at your earlier question with environments? At least several of them should do environments too. An example environment was even shown in one of the answers there. Apr 24, 2015 at 11:07
  • @PaulGessler If you are referring to tcolorbox, it is not an option due to compatibility issues with hyperref. Apr 27, 2015 at 12:32
  • What about the solution with package example: \begin{example} \fbox{\begin{minipage}{4cm} \raggedright \tiny\lipsum[2] \end{minipage}} \end{example} Apr 27, 2015 at 12:51
  • 2
    @macmadness86: What's the precise problem between tcolorbox and hyperref?
    – user31729
    Apr 27, 2015 at 16:21
  • Have you looked at the environ package? (If I'm remembering it right.)
    – cfr
    Apr 28, 2015 at 0:51

4 Answers 4


I did not use the OP's code, but I show here how the filecontents, etoolbox and verbatimbox packages can be used to both typeset some code and display the source, all in one environment.

EDITED to include it in a single environment, using the syntax


I set the output using my own simplistic format, but that can be changed to suit the format of the user.

I should note that I used \verbfilebox to set the code listing, which allows me to \fbox the result, and will prevent page breaks mid code. If one wanted to code to be able to break pages (using a full \textwidth listing), one could use the \verbfilenobox macro instead.

\parindent 0pt
  \textbf{Example:} \input{tmp}\par\bigskip
\begin{enviro}{tabular}{This is the standard latex environment for 
  tabular data.}
My & tabular\\
is & here\\

enter image description here

  • Is it possible to have LaTeX automatically delete the tmp.tex file after compilation? May 4, 2015 at 9:08
  • 1
    @macmadness86 With the so-called write18 feature of LaTeX (turned off by default for security reasons), one can invoke operating system commands from within a LaTeX compilation. But as an alternative, I would probably see if I could have tmp.tex open, not in the working directory, but in a system temp directory. That way, it wouldn't be deleted, but nor would it clutter your current directory. May 4, 2015 at 9:48

Doubt this is what the OP wants, but every now and then it's nice to do something from (almost) first principles, and convince myself that all that time I spent years ago reading Stephan von Bechtolsheim's TeX in practice wasn't completely wasted.

Note that the use of \scantokens assumes that etex is used.







{\vb@makeactive\ %
\gdef\vb@dospaces{\vb@makeactive\ \def {\vb@typesetspace}}%
\vb@makeactive\ \def {\space}%

\def\vb@typesetspace{\ }
\def\vb@typesettab{\ \ \ }







{This is the standard latex environment for tabular data.}
  col1 & col2 & col3 \\
  dat1 & dat2 & dat3 \\
  dat4 & dat5 & dat6 \\


enter image description here

  • The time you spent reading Stephan von Bechtolsheim's "TeX in practice" wasn't completely wasted :D Apr 30, 2015 at 5:57

My solution uses the temporary external file. The data are scanned in verbatim mode and saved to the file. Then the file is read with normal catcode settings, so the normal output is printed. Then the data from verbatim scan is used for verbatim output.


\def\begverbA{\bgroup \setverb \obeyspaces \obeylines \begverbB}
\edef\tmp{\def\noexpand\begverbB##1\bslash end\string{showcode\string}##2}
   %%% normal output:
   {\newlinechar=`\^^J \obeylinesJ
   \input verb-tmp.tex
   %%% verbatim output:
{\catcode`\^^M=\active \gdef\obeylinesJ{\catcode`\^^M\active \def^^M{^^J}}}


                {This is the standard \LaTeX\ environment for tabular data.}
  col1 & col2 & col3 \\
  dat1 & dat2 & dat3 \\
  dat4 & dat5 & dat6 \\

  • Could you explain the macros that do the heavy lifting? \setverb sets all chars to the same cat code (12), right? That is about as far as I got. What else is interesting here? \begverbA is the verbatim code? What is the purpose of \edef\bslash{\expandafter\tmp\string\\}? May 10, 2016 at 19:44

Using a hack of the example package.

It could be a good idea make directly your own package based in example.sty instead of muddle the preamble on this way.


\columnsep .1\textwidth  
      \@makeother\"\let\do\@makeother \dospecials
      \obeylines \obeyspaces
      \@ignoretrue \copy_line

      \hrule height\z@
      \input \example_name
      \hrule height\z@

      \hrule height\z@
         \frenchspacing \@vobeyspaces \@vobeytabs
         \input \example_name
      \hrule height\z@



\begin{example}{tabular}{This is the standard \LaTeX\ environment for tabular data, with optional horizontal and vertical lines.}
col1 & col2 & col3 \\
dat1 & dat2 & dat3 \\
dat4 & dat5 & dat6 \\

\begin{example}{quote}{This is a  standard \LaTeX\ environment for quotes.}
This is main text.
This is a quote
This is again the main text.

  • Hey that looks great. Could you elaborate as to where the example hack starts and ends? What are the \chardefs doing? Normally set special characters to have class 11 (letter) for verbatim information, right? May 4, 2015 at 7:09
  • @macmadness86 the whole preamble is the hack. The \chardefseries is the same that inside the example.sty, but I have to load again to redefine \example. The hack of \example is mainly add two arguments (the original does not support any) and the code for the format of the header (the rules and so on). \ExampleSet and \ExampleVerb are almost the same that the originals, excepting the width of the minipages and the background color using \fcolorboxes.
    – Fran
    May 4, 2015 at 14:23

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .