Context: I'm trying to write a package that will redefine various (Xe)LaTeX macros to facilitate automatically copying text from the rendered PDF document and pasting it, with minimal modifications, in a plain text document for inclusion/versioning in a git repo. I've tried for years to use pandoc et al. but that won't fit the bill because I rely heavily on biber+biblatex. It turns out that copying and pasting from macOS Preview works surprisingly well once you change the fonts to something “dumb” with no ligatures and change page dimensions to use an absurdly large width.

My problem is with (numbered) equations. I'd like to redefine the equation environment in some way so that instead of rendering math it displays the source code instead, using something like \verb/verbatim. Ideally, that would not require modifying the original source code in any way (the redefinition should happen in a package, so that the source code still uses normal \begin{equation}...\end{equation})


I tried this:


But I'm getting the following error:

./manuscript.tex:414: Missing $ inserted.
<inserted text> 
l.414 \begin{equation}

Not sure how to address this? My knowledge of the underlying TeX is minimal.

  • 2
    you could try \usepackage{fancyvrb}\DefineVerbatimEnvironment{equation}{Verbatim}{} May 20, 2023 at 8:50
  • @UlrikeFischer good idea, not sure how the verbatim \label will or will not be a disturbance in output from @Mathieu point of view. And \eqref/\ref will not work though.
    – user691586
    May 20, 2023 at 9:08
  • That would work very well, except that it removes the equation number and the corresponding ability to use \label, \ref.
    – Mathieu
    May 20, 2023 at 9:11
  • well if you put everything in a verbatim environment then you will loose the label anyway. May 20, 2023 at 9:53
  • See my answer below. Thanks for pointing me in the right direction.
    – Mathieu
    May 20, 2023 at 14:25

1 Answer 1


The code below works well enough for my purpose. It starts by detokenizing the contents of the equation to display it in plain text in a reproducible manner and then creates a numbered equation containing the original contents, hidden away in a virtual box.





You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .