# How to automatically escape special characters in your own environment?

I need to create my environment for examples of source codes. I want it to look like this:

Code example:

------------------------------------------------

# This is the code example

echo 'Hello'

------------------------------------------------

The problem is, that I don't know, how to force it to escape special characters (hash especially) automatically, so the users don't have to type backslash every time they want to write the hash.

### My code

\def\terminalText#1\end{\hspace*{2em}\texttt{#1}\\\end}

\newenvironment{codeExample}{
\vspace*{1.5em}
\noindent
Code example:\\
\hspace*{1.3em}\hdashrule[0.5ex]{435pt}{0.9pt}{1.5mm}\\
\terminalText}
{\hspace*{1.3em}\hdashrule[0.5ex]{435pt}{0.9pt}{1.5mm}}


This code is working well, untill I want to write any special character inside of the environment, then I'm getting this error:

! Paragraph ended before \terminalText was complete.

I've even tried to make some kind of wrapper to the verbatim environment, according to the question verbatim useable with a newenvironment definition?, but it's giving me this error:

! LaTeX Error: \begin{codeExample} on input line 535 ended by \end{verbatim}.

### My 2nd code

\newenvironment{codeExample}{
\vspace*{1.5em}
\noindent
Code Example:\\
\hspace*{1.3em}\hdashrule[0.5ex]{435pt}{0.9pt}{1.5mm}\\
\verbatim
}
{%
\endverbatim
\hspace*{1.3em}\hdashrule[0.5ex]{435pt}{0.9pt}{1.5mm}}

• Instead of reinventing the wheel: Wouldn't the listings package be easier to use? – user31729 Aug 3 '15 at 7:03

Using verbatim package the code for the second example works out of the box (if dashrule package is included as well)

\documentclass{article}

\usepackage{verbatim}
\usepackage{dashrule}

\newenvironment{codeExample}{
\vspace*{1.5em}
\noindent
Code Example:\\
\hspace*{1.3em}
\hdashrule[0.5ex]{435pt}{0.9pt}{1.5mm}

\verbatim
}
{%
\endverbatim
\hspace*{1.3em}
\hdashrule[0.5ex]{435pt}{0.9pt}{1.5mm}
}

\begin{document}

\begin{codeExample}
# This is the code example

echo 'Hello'
\end{codeExample}

\end{document}


Please consider listings package as a 'better' and cleaner way to typeset code examples.

Edit Here's an example with the tcolorbox output for listings.

\documentclass{article}

\usepackage{verbatim}
\usepackage{dashrule}

\usepackage[most]{tcolorbox}

\newenvironment{codeExample}{
\vspace*{1.5em}
\noindent
Code Example:\\
\hspace*{1.3em}
\hdashrule[0.5ex]{435pt}{0.9pt}{1.5mm}

\verbatim
}
{%
\endverbatim
\hspace*{1.3em}
\hdashrule[0.5ex]{435pt}{0.9pt}{1.5mm}
}

\newtcblisting[auto counter]{codeex}[1][]{%
arc=0pt,
auto outer arc,
colbacktitle=yellow,
coltitle=black,
title={Code Example \thetcbcounter},
listing options={language=bash},
listing only,
lowerbox=ignored,
before upper=\hdashrule[0.5ex]{\textwidth}{0.9pt}{1.5mm},
after upper=\hdashrule[0.5ex]{\textwidth}{0.9pt}{1.5mm}
#1
}

\begin{document}

\begin{codeExample}
# This is the code example

echo 'Hello'
\end{codeExample}

\begin{codeex}
echo 'Hello'

\end{codeex}

\end{document}


• This one is stille giving me the ! LaTeX Error: \begin{codeExample} on input line 535 ended by \end{verbatim} error. But you're right, I'll use listings package later, it looks great. I had no idea about it before :-) – Eenoku Aug 3 '15 at 7:52
• Apart from listings, there is also the nice tcolorbox package which provides nice boxing and breaking of listings etc. But do you have a relatively new TeX distribution? The code above works without problems – user31729 Aug 3 '15 at 7:54
• Thank you :-) I have, I think, the problem is maybe caused by TexMaker... – Eenoku Aug 3 '15 at 10:23
• @MartinBeseda: My experience is, that all those editors cause more problems than using a direct command line compile, therefore I don't use them. Thanks for accepting! – user31729 Aug 3 '15 at 10:24