pythontex에서 섹션 번호를 Python에 전달하는 방법은 무엇입니까?

pythontex에서 섹션 번호를 Python에 전달하는 방법은 무엇입니까?

pythontex를 사용하여 JSON 파일에 일부 매개변수를 작성하고 있지만 섹션 번호 형식(예: \thesection 또는 \thesubsubsection, 페이지 번호 또는 기타 카운터)으로 컨텍스트 정보를 추가하고 싶습니다.

섹션 카운터의 값을 Python에 전달하는 간단한 방법이 있습니까?

  • 요셉

답변1

이를 수행할 수 있는 방법은 다양합니다. 여기에 한 가지 접근 방식이 있습니다. 다른 접근 방식은 \setpythontexcontextpythontex 설명서를 참조하세요.

pythontex 명령은 \pyc코드를 그대로 Python으로 보내도록 설계되었습니다. 여기서의 비결은 \thesectionPython 변수에 저장하기 전에 완전히 확장하는 것입니다.

\documentclass{article}
\usepackage{pythontex}
\makeatletter
\newcommand{\sectopy}{%
  \edef\sectopy@val{\thesection}%
  \expandafter\sectopy@i\expandafter{\sectopy@val}}
\def\sectopy@i#1{\pyc{section = #1}}
\makeatother

\begin{document}

\section{Section}

\sectopy

The section is: \py{section}

\end{document} 

관련 정보