3

Is there an equivalent way to write the body of my environment to a file without filecontents using TeX primatives?

morewrites or filecontents limitation

I suspect a limitation that these packages do not play well together. Using the paired macros from filecontents package shown below, I can write the contents (body) of an environment to a file called tmp.tex:

\@tempswafalse\filec@ntents{tmp}

end

\endfilecontents

For another part of my project, I ran out of writes, so I added \usepackage{morewrites}. This works for my TeX primative writes, but it does not help when using the filecontents package.

Example Code:

One too many writes...

\documentclass{article}
\usepackage{etoolbox}
\usepackage{xcolor}
\usepackage{verbatimbox}
\usepackage{tikz}
\usepackage{filecontents}
\usepackage{morewrites}% Does not support extend filecontents package, limited \@tempswafalse\filec@ntents{tmp} writes


% I tried putting this into a loop, but I failed
\newwrite\writeA
\newwrite\writeB
\newwrite\writeC
\newwrite\writeD
\newwrite\writeE
\newwrite\writeF
\newwrite\writeG
\newwrite\writeH
\newwrite\writeI
\newwrite\writeJ
\newwrite\writeK
\newwrite\writeL % Comment this out to compile, one too many writes
%\newwrite\writeM
%\newwrite\writeN
%\newwrite\writeO
%\newwrite\writeP
%\newwrite\writeQ
%\newwrite\writeR

% Environments
\makeatletter
\newenvironment{environment}[2]
{\gdef\environame{#1}\gdef\envirodescription{#2}%
  \@tempswafalse\filec@ntents{tmp}
  }%
{\endfilecontents}
\makeatother

\AfterEndEnvironment{environment}{\setenviro}

\newcommand\setenviro{%
  \def\hrulespacedist{2mm}
  \vspace{\hrulespacedist}\hrule\vspace{\hrulespacedist}
  \texttt{\environame}
  \hfill
  \begin{minipage}[t]{.75\textwidth}
  \envirodescription
  \end{minipage}\par\bigskip
  \textcolor{green!30!black}{\textbf{Example:}}\par\bigskip
  \input{tmp}
  \par\bigskip
  \textcolor{red!80!black}{\textbf{Code:}}\par\bigskip
  \verbfilebox[\footnotesize]{tmp}
 \theverbbox
 \vspace{\hrulespacedist}\hrule\vspace{\hrulespacedist}
}



\begin{document}

\begin{environment}{enumerate}{An enumerated/numbered list.}
  \begin{enumerate}
    \item First item
  \end{enumerate}
\end{environment}

\end{document}

Output

enter image description here

Related

  • The content must be written verbatim, I expect, not just catching the content? –  Jun 03 '16 at 11:37
  • why do you need so many writes? it seems like you should only need 1? You need to be writing to 17 different files at the same time? – David Carlisle Jun 03 '16 at 16:30
  • I LOVE writes. (In reality I have more than that even--doing all kinds of cool stuff!) – Jonathan Komar Jun 03 '16 at 16:31
  • @ChristianHupfer Yes, you're right. I am typesetting s LaTeX manual with many custom environments and commands. – Jonathan Komar Jun 03 '16 at 16:37
  • @macmadness86: Perhaps my answer here solves your request, however, it's a different approach: http://tex.stackexchange.com/questions/279068/how-to-display-latex-code-and-the-typeset-version-next-to-each-other-without-rep/279069#279069 –  Jun 03 '16 at 16:44

1 Answers1

3

Seems odd to need to write 17 files concurrently but anyway I think you can make filecontents use a fixed write stream before morewrites starts redefining things:

\documentclass{article}
\usepackage{etoolbox}
\usepackage{xcolor}
\usepackage{verbatimbox}
\usepackage{tikz}
\usepackage{filecontents}
\newwrite\fcwrite
\makeatletter
\let\zzzz\filec@ntents
\def\filec@ntents{\def\chardef##1\write{\let\reserved@c\fcwrite}\zzzz}
\makeatother

\usepackage{morewrites}% Does not support extend filecontents package, limited \@tempswafalse\filec@ntents{tmp} writes


% I tried putting this into a loop, but I failed
\newwrite\writeA
\newwrite\writeB
\newwrite\writeC
\newwrite\writeD
\newwrite\writeE
\newwrite\writeF
\newwrite\writeG
\newwrite\writeH
\newwrite\writeI
\newwrite\writeJ
\newwrite\writeK
\newwrite\writeL % Comment this out to compile, one too many writes
\newwrite\writeM
\newwrite\writeN
\newwrite\writeO
\newwrite\writeP
\newwrite\writeQ
\newwrite\writeR

% Environments
\makeatletter
\newenvironment{environment}[2]
{\gdef\environame{#1}\gdef\envirodescription{#2}%
  \@tempswafalse\filec@ntents{tmp}
  }%
{\endfilecontents}
\makeatother

\AfterEndEnvironment{environment}{\setenviro}

\newcommand\setenviro{%
  \def\hrulespacedist{2mm}
  \vspace{\hrulespacedist}\hrule\vspace{\hrulespacedist}
  \texttt{\environame}
  \hfill
  \begin{minipage}[t]{.75\textwidth}
  \envirodescription
  \end{minipage}\par\bigskip
  \textcolor{green!30!black}{\textbf{Example:}}\par\bigskip
  \input{tmp}
  \par\bigskip
  \textcolor{red!80!black}{\textbf{Code:}}\par\bigskip
  \verbfilebox[\footnotesize]{tmp}
 \theverbbox
 \vspace{\hrulespacedist}\hrule\vspace{\hrulespacedist}
}



\begin{document}


\begin{environment}{enumerate}{An enumerated/numbered list.}
  \begin{enumerate}
    \item First item
  \end{enumerate}
\end{environment}

\end{document}
David Carlisle
  • 757,742