\newcommand{\test}[2]{%
\begin{itemize}
\pyc{strs = u"#1".split(u"#2");}
\pyc{for s in strs: print("\item " + s);}
\end{itemize}
}
使用此命令时,我得到以下输出:
可能缺少 \item。
梅威瑟:
\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}
答案1
问题是,在运行 PythonTeX 之前编译该文件时,itemize
环境是空的,因此\@noitemerr
会执行。
定义一个pyitemize
禁用它的环境。
\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}
没有 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}
顺便说一下,
\defaultfontfeatures{Ligatures=TeX,Mapping=tex-text}
是多余的:它对默认情况下已开启的设置执行两次。