Custom environment with parameter: You can't use `macro parameter character #' in vertical mode

The definition of

\newenvironment{TextFile}[1]{\ttfamily}{\par}
{
    #1\\[1ex]
    \begin{center}
    \begin{tabular}{|p{0.9\textwidth}|}
    \hline
    \\
}
{ 
    \\ \\
    \hline
    \end{tabular} 
    \end{center}
}

is wrong, at least the parts with { #1\\ }{...}, which are surely meant as the real document body, not something outside after the {\par} statement which is the end code part of the environment.

This way #1 ... etc. is meant as typesetting after the environment definition which must fail unless the definition is wrapped itself in another command or environment.

\documentclass{article}



\newenvironment{TextFile}[1]{\ttfamily%
    #1 \\[1ex]
    \begin{center}
    \begin{tabular}{|p{0.9\textwidth}|}
    \hline
    \\
}{% 
    \\ \\
    \hline
    \end{tabular} 
    \end{center}
    \par
}

\begin{document}

\begin{TextFile}{Foo}
Learning \LaTeXe\ is fun!
\end{TextFile}

\end{document}

enter image description here


Your definition doesn't work because \newenvironment has the following syntax:

\newenvironmnt{<envname>}[<parms>][<opt>]
  {<begin envname>}% \begin{<envname>}
  {<end envname>}% \end{<envname>}

You've used it with no optional arguments (so [<opt>] has been removed) but supplied four mandatory arguments instead of two (the two being {<begin envname>} and {<end envname>}). Any of the arguments specified in [<parms>] are only available within {<begin envname>}, yet you've attempted to use it inside the third mandatory argument, where it doesn't exist.


The following approaches ensure that the title and subsequent block stay together and fit within the text block width.

enter image description here

\documentclass{article}

\usepackage{tabularx}

\newenvironment{TextFile}[1]
  {% \begin{TextFile}{#1}
   \par\ttfamily
   \noindent
   % http://tex.stackexchange.com/a/42331/5764
   \tabularx{\linewidth}{|X|}
     \multicolumn{1}{c}{#1} \\[1ex]
     \hline \\
  }{% \end{TextFile}
     \\ \\
     \hline
    \endtabularx
    \par\medskip
  }

\begin{document}

\begin{TextFile}{My Title}
Foo \\
Bar
\end{TextFile}

\end{document}

Another option using listings:

enter image description here

\documentclass{article}

\usepackage{listings}

\lstnewenvironment{TextFile}[1]
  {\lstset{
    basicstyle = \ttfamily,
    frame      = lrtb,
    title      = {\ttfamily #1}}}
  {}

\begin{document}

\begin{TextFile}{My Title}
Foo
Bar
\end{TextFile}

\end{document}

Tags:

Environments