3
\newcommand{\test}[2]{%
    \begin{itemize}
    \pyc{strs = u"#1".split(u"#2");}
    \pyc{for s in strs: print("\item " + s);}
    \end{itemize}
}

When using this command, I get the folowing output:

perhaps missing \item.

MWE:

\documentclass[a5paper]{extbook}

\usepackage{geometry}
\usepackage{polyglossia}
\usepackage{fontspec}

\usepackage[pyfuture=all]{pythontex}

\setmainlanguage{russian}
\setotherlanguage{english}
\defaultfontfeatures{Ligatures=TeX,Mapping=tex-text}
\setmainfont{Times New Roman}


\newcommand{\test}[2]{%
    \begin{itemize}
    \pyc{strs = u"#1".split(u"#2");}
    \pyc{for s in strs: print("\item " + s);}
    \end{itemize}
}

\begin{document}

\test{test1, test2, test3, тест4}{,}

\end{document} 
  • Maybean MWE would be helpful – Moriambar Mar 24 '17 at 13:11
  • 1
    @ilFuria added example usage – Alexander Biryukov Mar 24 '17 at 13:20
  • 1
    Another option would be to print the \begin{itemize} and \end{itemize} from Python as well, so that the entire command contents comes from Python, and you don't end up with only parts of it existing. – G. Poore Mar 25 '17 at 15:15
1

The problem is that, when the file is compiled before running PythonTeX, the itemize environment is empty, so \@noitemerr is executed.

Define a pyitemize environment that disables it.

\documentclass[a5paper]{extbook}

\usepackage{geometry}
\usepackage{polyglossia}
\usepackage{fontspec}

\usepackage[pyfuture=all]{pythontex}

\setmainlanguage{russian}
\setotherlanguage{english}
\setmainfont{Times New Roman}

\makeatletter
\newenvironment{pyitemize}
 {\def\@noitemerr{}\itemize}
 {\enditemize}
\makeatother

\newcommand{\test}[2]{%
    \begin{pyitemize}
    \pyc{strs = u"#1".split(u"#2");}
    \pyc{for s in strs: print("\item " + s);}
    \end{pyitemize}
}

\begin{document}

Some text before

\test{test1, test2, test3, тест4}{,}

\end{document}

enter image description here

You can do the same without PythonTeX.

\documentclass[a5paper]{extbook}

\usepackage{geometry}
\usepackage{polyglossia}
\usepackage{fontspec}
\usepackage{xparse}

\setmainlanguage{russian}
\setotherlanguage{english}
\setmainfont{Times New Roman}

\ExplSyntaxOn
\NewDocumentCommand{\test}{mm}
 {
  \begin{itemize}
  \seq_set_split:Nnn \l_tmpa_seq { #2 } { #1 }
  \seq_map_inline:Nn \l_tmpa_seq { \item ##1 }
  \end{itemize}
 }
\ExplSyntaxOff

\begin{document}

Some text before

\test{test1, test2, test3, тест4}{,}

\end{document}

By the way, the line

\defaultfontfeatures{Ligatures=TeX,Mapping=tex-text}

is redundant: it does twice a setting that's already on by default.

  • I know it's possible without pythontex, even without xparse with pure LaTeX, like this: latex \def\menu#1{% \begin{itemize} \gdef\firstelement{1} \foreach \e in {#1}{% \ifnum\firstelement=0\fi \item \e% \gdef\firstelement{0}% } \end{itemize} } Thing is, I know python, and I'm not that good at LaTeX macros, it's easier to debug and produce python code for me. So I'm trying to replace simple macros with python scripts, and then go futher with more complex examples. – Alexander Biryukov Mar 24 '17 at 14:18
  • @Sanya_rnd No problem at all! I just suggested a possibly simpler approach. – egreg Mar 24 '17 at 14:22

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.