7

I’d like to have a solution environment that does the following:

  • It writes the contents of the environment to an external file \jobname.py. All the solutions should be in the same file \jobname.py.
  • The content of this environment (the solution) will be Python code and will include # characters and tab characters. The solution should not be displayed in the PDF, it should only be written in the external file.
  • (If writing actual tab characters isn’t possible, I can work around it by replacing tabs with consecutive spaces.)
  • Before writing the environment’s contents to \jobname.py, it should before write in \jobname.py the following line:

## Exercise \numberExercise,

where \numberExercise is a macro that returns the exercise number (I will define this macro, but for testing you can assume \def\numberExercise{2.1}).

The goal of this environment is to put directly the solutions to the exercises in my LaTeX document and to be able to create a .py file from the .tex document.

I tried to write this environment but couldn’t get it to work.

Could you help me?

Please feel free to ask if anything is unclear.

Here is a MWE:

\documentclass{article} \def\numberExercise{2.1} \newenvironment{solution} {...} {...} \begin{document} Write a function \texttt{facto(n)} that returns the factorial of $n$. \begin{solution} # We proceed recursively def facto(n): if n == 0: return 1 else: return facto(n-1)*n \end{solution} \end{document} 
2
  • 1
    Much of this is unclear, sorry. Do you want a unique file for all the exercises? Commented Oct 11 at 10:51
  • Yes, a unique file for all the solutions. I will try to make it clearer. Commented Oct 11 at 11:31

1 Answer 1

9

You can use the c argument type (see texdoc usrguide).

\documentclass{article} \newcounter{solution} \ExplSyntaxOn \NewDocumentEnvironment{solution}{c} { \stepcounter{solution} \colas_python_solution:n {#1} } {} \iow_new:N \g_colas_python_out_iow \iow_open:Nn \g_colas_python_out_iow { \c_sys_jobname_str.py } \cs_new_protected:Nn \colas_python_solution:n { \group_begin: \iow_now:Ne \g_colas_python_out_iow { \c_hash_str\c_hash_str\c_space_tl Exercise~\thesolution ^^J ^^J } \cs_set:Npn \obeyedline { ^^J } \iow_now:Ne \g_colas_python_out_iow {#1} \group_end: \begin{flushleft} \textbf{Exercise~\thesolution}\par\nopagebreak\medskip \tl_set:Nn \l__colas_python_exercise_tl {#1} \tl_replace_all:Nen \l__colas_python_exercise_tl { \char_generate:nn {`\ } {12} } { \ } \tl_replace_all:Nen \l__colas_python_exercise_tl { \char_generate:nn {`\^^I } {12} } { \ \ \ \ } \ttfamily \l__colas_python_exercise_tl \end{flushleft} } \ExplSyntaxOff \begin{document} \begin{solution} # We proceed recursively def facto(n): if n == 0: return 1 else: return facto(n-1)*n \end{solution} \end{document} 

Note: the last line has, in my experiment, two TABS at the left, but likely they'll be converted to spaces here.

PDF output

output

Written \jobname.py file

## Exercise 1 # We proceed recursively def facto(n): if n == 0: return 1 else: return facto(n-1)*n 
4
  • Thank you for your answer. The solution should not be displayed in the PDF, it should only be written in the external file. Commented Oct 12 at 7:56
  • 1
    @Colas Remove the part from \begin{flushleft} to \end{flushleft}. Commented Oct 12 at 12:44
  • 2
    I think that the specifier c of \NewDocumentEnvironment is available in the current stable version of LaTeX (2025-06-01). Commented Oct 12 at 19:05
  • @F.Pantigny I missed it! Commented Oct 12 at 19:58

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.