1

I have read in the PythonTeX manual about how to pass LaTeX values to PythonTeX, but I cannot find any documentation (or a way) to do the reverse. For example, in the LaTeX package datenumber, one passes <year>, <month>, and <day> to \setdatenumber{<year>}{<month>}{<day>} in order to set \thedatenumber, a counter defined by the package. I want to do something like this:

\documentclass{article}
\usepackage{pythontex}
\usepackage{datenumber}

\begin{document}

\pyc{year = 2015}
\pyc{day = 2}
\pyc{month = 3}

\setdatenumber{\py{year}}{\py{month}}{\py{day}}

\end{document}

But, datenumber gets frustrated and tells me its missing number. Is the problem the way I'm using PythonTeX or just the way datenumber works?

Thank you in advance.

fiziks
  • 345
  • 2
  • 8

1 Answers1

3

datenumber probably expects numbers, or at least fully expandable macros. PythonTeX macros are not fully expandable.

Getting this to work is similar to using PythonTeX with siunitx. You could create a command that wraps the datenumber command. The wrapper command takes the arguments, performs any processing, and then sends back a datenumber command using the processed results. You probably want something like this.

\documentclass{article}
\usepackage{pythontex}
\usepackage{datenumber}

\begin{document}

\newcommand{\pysetdatenumber}[3]{%
  %Store variables on Python side
  \pyc{year = #1; day = #2; month = #3}%
  %Send back datenumber command to use variables on TeX side
  %Could also use \pyc with print()
  \py{r'\setdatenumber{{{0}}}{{{1}}}{{{2}}}'.format(year, day, month)}}

\pysetdatenumber{2015}{3}{2}

\datedate

\end{document}
G. Poore
  • 12,417
  • Thanks so much. Your solution works perfectly. But, I'm not understanding why you have to put two sets of delimiters, e.g., {{0}}. Why does one set cause an error? – fiziks Mar 02 '15 at 19:05
  • @fiziks In Python's format() method, replacement fields are delimited by braces, and braces are doubled to be literal. So {0} is field zero, and {{ is a literal left brace. {{{0}}} is field zero, surrounded by a literal brace on each side. – G. Poore Mar 02 '15 at 20:11