5

I can't get the syntax right. I am trying to use this answer below to help me.

how to use conditional to define different \begin and \end environment at compile time

I am trying to setup an environment for tex4ht vs. pdflatex. If tex4ht is running, then the environment will be

\begin{Verbatim}
   .......
\end{Verbatim}

If pdflatex is running, then it will be

\begin{Verbatim}[samepage,fontsize=\small,frame=single]
   .......
\end{Verbatim}

I am not able to figure the API to \NewEnviron to pass those additional arguments above inside the [....]

Here is a MWE

\documentclass{article}%    
\usepackage{fancyvrb}
\usepackage{array}%for m option

\usepackage{environ}
\NewEnviron{X}[4][]{%  takes 4 arguments, samepage,fontsize=..,frame=...,Verbatim
  \ifdefined\HCode %htlatex
    \begin{#4}
      \BODY
    \end{#4}
  \else% pdflatex
    \begin{#4}[#1,#2,#3]  %is this right?
      \BODY
    \end{#4}
  \fi%
}

\begin{document}

\begin{tabular}[c]{|m{4in}|m{2.5in}|}\hline
\textbf{Mathematica}
\begin{X}[samepage][fontsize=\small][frame=single]{Verbatim}  %is this right?

    {(10 E^(-t/10) HeavisideTheta[t] 
       Sin[(3 Sqrt[11] t)/10])/(3 Sqrt[11])}   some Verbatim stuff

\end{X}
&
   some text
\\\hline
\end{tabular}

\end{document}

The above should become the following (if pdflatex is running)

\begin{tabular}[c]{|m{4in}|m{2.5in}|}\hline
\textbf{Mathematica}
\begin{Verbatim}[samepage,fontsize=\small,frame=single]

   {(10 E^(-t/10) HeavisideTheta[t] 
       Sin[(3 Sqrt[11] t)/10])/(3 Sqrt[11])}

\end{Verbatim}
&
   some text
\\\hline
\end{tabular}

and if tex4ht if running, it should become

\begin{tabular}[c]{|m{4in}|m{2.5in}|}\hline
\textbf{Mathematica}
\begin{Verbatim}

    {(10 E^(-t/10) HeavisideTheta[t] 
       Sin[(3 Sqrt[11] t)/10])/(3 Sqrt[11])}

\end{Verbatim}
&
   some text
\\\hline
\end{tabular}

I tried calling it as follows

\begin{X}[samepage,fontsize=\small,frame=single]{Verbatim} 

But that did not help. I keep getting syntax error Missing $ inserted \end{X} and extra } etc....

Also tried

\begin{X}[samepage],[fontsize=\small],[frame=single]{Verbatim} 

What is the correct syntax to setup this environment? Using texlive 2013 and Miktex

  • You can't use environ for defining verbatim environments. – egreg May 23 '14 at 10:37
5

You can't define verbatim environments using \NewEnviron, because those absorb the environment's content as an argument, so no category code changing, essential for verbatim, can be performed.

There is a much simpler way: define the environment in two different ways, rather than one environment that changes behavior.

\documentclass{article}
\usepackage{fancyvrb}
\usepackage{array}%for m option


\ifdefined\HCode % tex4ht
  \DefineVerbatimEnvironment{X}{Verbatim}{}
\else
  \DefineVerbatimEnvironment{X}{Verbatim}{samepage,fontsize=\small,frame=single}
\fi

\begin{document}

\begin{tabular}[c]{|m{3in}|m{1in}|}\hline
\textbf{Mathematica}
\begin{X}
    {(10 E^(-t/10) HeavisideTheta[t]
       Sin[(3 Sqrt[11] t)/10])/(3 Sqrt[11])}
\end{X}
&
some text\\
\hline
\end{tabular}

\end{document}
  • Thanks. This seems to have worked but when I started adding it to my main document, now I get these error File ended while scanning use of \FancyVerbGetLine, the problem is I have no idea which line number it is coming from. I have to roll back my changes and see. Package hyperref Warning: Token not allowed in a PDF string (PDFDocEncoding): (hyperref) removing math shift' on input line 2222. ) Runaway argument?! File ended while scanning use of \FancyVerbGetLine.<inserted text> \par <*> KERNEL.tex?` – Nasser May 23 '14 at 12:17
  • @Nasser Sorry, but it's impossible to say anything sensible with so little information. Besides, hyperref isn't mentioned in your MWE. – egreg May 23 '14 at 12:21
  • This was meant as a FYI and not as anything specific. Your fix works fine in the MWE, but for some reason I am getting this error when I added the changes to my main document. Will try to find what is the problem. hyperref has nothing to do with this, I just copied the last few lines from the log. The error is File ended while scanning use of \FancyVerbGetLine and will try to see why it shows up in my main document and not in the MWE. thanks. – Nasser May 23 '14 at 12:28
  • fyi, I redid the changes and do not see the error any more. I must have did something wrong myself before. It is all working well now. Thank you again for your help. – Nasser May 23 '14 at 19:37

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.