FancyVerb error when % at end, when using \DefineVerbatimEnvironment

This MWE gives an error when % is at the end the line as show below

\documentclass[11pt]{article}%
\usepackage{fancyvrb}
\DefineVerbatimEnvironment{Xsmall}{Verbatim}{fontsize=\small}
\begin{document}
\begin{Xsmall}
test
\end{Xsmall}%  ------> This causes the error
\end{document}


Then pdflatex gives

) (/usr/local/texlive/2015/texmf-dist/tex/latex/fancyvrb/fancyvrb.sty
Style option: fancyvrb' v2.7a, with DG/SPQR fixes, and firstline=lastline fix
<2008/02/07> (tvz)
(/usr/local/texlive/2015/texmf-dist/tex/latex/graphics/keyval.sty)) (./foo.aux)
! FancyVerb Error:
Extraneous input %\end{}' between \end{Xsmall} and line end
.
\FV@Error ... {FancyVerb Error:
\space \space #1
}
l.11 \end{Xsmall}%


Of course the solution is not to put % there. But life is not that simple. I use another software to help me generate these tex file. And this software keeps adding this % there each time. If I remove it, and reload the tex file again into it, it will add this % again.

My question is, is there something I can change in the definition of the Xsmall above, such that adding this % do not cause a problem compiling it? Some kind of a trick or option or macro?

Again, I have to use this other software (it is Scientific word) but I also need to use this macro and be able to compile my file using tex live. There is no way to tell SW not to keep adding % at end of lines like this. I tried. It just keeps adding % each time.

Here is file list

 article.cls    2014/09/29 v1.4h Standard LaTeX document class
size11.clo    2014/09/29 v1.4h Standard LaTeX file (size option)
fancybox.sty    2010/05/15 1.4
fancyvrb.sty    2008/02/07
keyval.sty    2014/10/28 v1.15 key=value parser (DPC)


TL 2015 on Linux.

• You could try with a search-replace function with sed (or any other programs available in your system) before each compilation to remove the % from the document. I tried this on my terminal: sed -i 's/end{Xsmall}\%/end{Xsmall}/g' <file> | pdflatex <file> – Sunilkumar KS Nov 27 '15 at 7:04
• I'm afraid that the solution is “don't type % in that position”. Sorry. – egreg Nov 27 '15 at 10:05

Thanks for help from Scientific Word support, they gave me a work around for the Latex problem in my question. I post it here, in case someone in the future have to deal with it. Here is the answer

Add a space after the encapsulated TeX field. The % at the end suppresses a normally unwanted white space in the typeset results.

So I just needed to add one space here:

\begin{Xsmall}
test
\end{Xsmall} <----- ADD ONE SPACE HERE


Now Scientific word do not add a % !

Case closed. So I get clean compile.

• thanks for posting the fix. i tried running latex putting a space between the } % and it had no useful effect. so the workaround has to be applied in sw -- it the file gets to tex, it's too late. (we've had to get sw to make some fixes, and they've been pretty good about it; the worst problem was when they left expansions from tcilatex, complete with comments, in index entries. never put a % comment in an index entry! it gets through makeindex and blows up in the middle of \printindex; very confusing!) – barbara beeton Dec 1 '15 at 21:41
• @barbarabeeton yes, this fix is specific for SW. in SW it has what it calls a TexField. It is used to allow one to enter raw latex in the document itself. But SW was adding % to the end of the line, even though the raw Latex was inside a TexField. Adding one white space, told SW not to add % any more to end of line, when it saves the document as Latex file. So I am able to compile it using TexLive. This all applies to SW 5.5. They have a new major version called 6.0 now, released few months ago, which I do not use. Version 6 is based on XML. – Nasser Dec 1 '15 at 22:20
• @barbarabeeton if the Latex document in SW is saved as what is called Portable Latex, then tcilatex is not used. tcilatex is only used when one saves the document as SW/SWP/SN. I never do this. I always choose Portable Latex. – Nasser Dec 1 '15 at 22:24
• what you say is true, but some of the definitions from tcilatex used to be carried through to the "portable" version, inadvisedly. that has now been fixed, but it was a real problem for a while. fortunately, the support team was responsive once they saw the bad results. – barbara beeton Dec 1 '15 at 22:59

I was having similar problem with Sweave It was removed after I added the lines

\usepackage{fancyvrb}
\usepackage{fvextra}


to the preamble.