1

I would like to pass "variables" to my code.

Minimal example (example.tex):

\documentclass{article}
\usepackage[usefamily=bash]{pythontex}
\begin{document}
\bash|echo "\jobname"|
\end{document}

Execute pdflatex -shell-escape example.tex.

Seen content of example-minimal.pytxcode:

=>PYTHONTEX#bash#default#default#0#i#####4#
echo "\jobname"

Expected content of example-minimal.pytxcode:

=>PYTHONTEX#bash#default#default#0#i#####4#
echo "example.tex"

Background: I want to port my plantuml package from plain lua to pythontex to a) support pdflatex and b) to make use of the caching capabilities of it.

koppor
  • 3,252

2 Answers2

1

I'm not sure if this is what you'll eventually need, but for the present purpose it should work. The idea is to absorb the argument verbatim, then make a replacement of all sequences “backslash+letters” into the corresponding control sequence, then pass the whole thing to \bash.

\documentclass{article}
\usepackage[usefamily=bash]{pythontex}
\usepackage{xparse}

\ExplSyntaxOn
\NewDocumentCommand{\ebash}{v}
 {
  \tl_set:Nn \l_ebash_input_tl { #1 }
  \regex_replace_all:nnN { \\([[:alpha:]]*) } { \c{\1} } \l_ebash_input_tl
  \tl_set:Nx \l_ebash_input_tl { \l_ebash_input_tl }
  \exp_args:NV \bash \l_ebash_input_tl
 }
\ExplSyntaxOff

\begin{document}

\ebash|echo "\jobname"|

\end{document}

You may also be interested in https://tex.stackexchange.com/a/155436/4427

egreg
  • 1,121,712
0

Not a good answer and I think you shouldn't use it, but you could create a helper macro using expl3 that fully expands it's argument before giving it to \bash:

\documentclass{article}
\usepackage{expl3}
\usepackage[usefamily=bash]{pythontex}
\ExplSyntaxOn
\newcommand\expandBASH[1]{ \exp_args:Nx \bash { #1 } }
\ExplSyntaxOff
\begin{document}
\bash|echo "\jobname"|
\expandBASH{echo "\jobname"}
\end{document}

You could also provide different delimiters using xparse or \def.

Skillmon
  • 60,462