7

What is wrong with following document?

\documentclass{article}
\usepackage{xepersian}
\usepackage{verbatim}

\newenvironment{code}
 {\begin{latin}\begin{verbatim}}
 {\end{verbatim}\end{latin}}

\usepackage{enumitem}
\begin{document}
\begin{code}
hello world!
\end{code}
\end{document}

Error:

! File ended while scanning use of \next.
<inserted text> 
                \par 
<*> untitled-4.tex

I also tried this:

\documentclass{article}
\usepackage{listings}
\usepackage{xepersian}
\newenvironment{code}
 {\begin{latin}\begin{lstlisting}}
 {\end{lstlisting}\end{latin}}

\begin{document}

\begin{code}
hello world!
This is a code
\end{code}

\end{document}

But the output is not what I expect.

Alan Munn
  • 218,180
Real Dreams
  • 8,298
  • 12
  • 56
  • 78

1 Answers1

6

Verbatim environments are very special, and generally resist being the arguments of macros, which is effectively what you are doing when you try to put a verbatim environment inside another environment definition. For more details on this problem, see the TeX FAQ entry on the topic.

Instead, what you can do is hook into the beginning of the verbatim or lstlisting environments to switch into LTR mode. Here's an example:

\documentclass{article}
\usepackage{verbatim}
\usepackage{listings}
\usepackage{etoolbox}

\usepackage{xepersian} \AtBeginEnvironment{verbatim}{\setLTR\latinfont} \AtBeginEnvironment{lstlisting}{\setLTR\latinfont} \begin{document} \begin{verbatim} hello world! \end{verbatim} \begin{lstlisting} hello world! \end{lstlisting} این فقط یک آزمایش است \end{document}

output of code

Alan Munn
  • 218,180