
我正在嘗試egreg 很好的答案這裡在我的主文檔中,我發現它在那裡不起作用。事實證明,這是因為我碰巧使用了另一個breqn
我真正需要的包。
我不知道為什麼 breqn
會破壞命令。但這裡有一個 MWE
\documentclass[10pt,notitlepage]{article}
\usepackage{etoolbox}%see https://tex.stackexchange.com/questions/257621
\makeatletter
\newcommand{\shellcommand}[1]{\@@input"|#1"}
\makeatother
\usepackage{breqn}
\newcounter{c}
\begin{document}
\setcounter{c}{\shellcommand{id -g}}
\arabic{c}
\end{document}
使用編譯lualatex -shell-esc foo.tex
,錯誤是
(/usr/local/texlive/2015/texmf-dist/tex/latex/tools/calc.sty)) (./foo2.aux)
("|id -g!"id: invalid option -- '!'
Try 'id --help' for more information.
Runaway argument?
! Paragraph ended before \@calc@pre@scan was complete.
<to be read again>
\par
l.1
注意額外的“!”在那裡添加。
現在只需註釋該行\usepackage{etoolbox}
並再次編譯,它就可以工作並且我得到了預期的結果。沒有錯誤。
有沒有辦法保留breqn
並且仍然能夠使用egreg\shellcommand
命令?
去掉breqn
套件並編譯時:
>lualatex -shell-esc foo2.tex
This is LuaTeX, Version beta-0.80.0 (TeX Live 2015) (rev 5238)
\write18 enabled.
(./foo2.tex
LaTeX2e <2015/01/01> patch level 2
Babel <3.9l> and hyphenation patterns for 79 languages loaded.
(/usr/local/texlive/2015/texmf-dist/tex/latex/base/article.cls
Document Class: article 2014/09/29 v1.4h Standard LaTeX document class
(/usr/local/texlive/2015/texmf-dist/tex/latex/base/size10.clo))
(/usr/local/texlive/2015/texmf-dist/tex/latex/etoolbox/etoolbox.sty)
(./foo2.aux) ("|id -g") [1{/usr/local/texlive/2015/texmf-var/fonts/map/pdftex/up
dmap/pdftex.map}] (./foo2.aux))
264 words of node memory still in use:
2 hlist, 1 vlist, 1 rule, 2 glue, 40 glue_spec, 1 write nodes
avail lists: 1:4,2:12,3:3,4:22,6:11,7:1,9:6
<</usr/local/texlive/2015/texmf-dist/fonts/type1/public/amsfonts/cm/cmr10.pfb>
Output written on foo2.pdf (1 page, 8298 bytes).
Transcript written on foo2.log.
>
pdf 包含1000
預期的內容。
Linux Mint 12.2 上的 TL 2015
答案1
該錯誤不是由於breqn
,而是calc
由它自動加載的。它與任何一個都無關etoolbox
,因為\@@input
它是原語\input
並且是在 LaTeX 核心中定義的。
這是一個非常小的例子:
\documentclass{article}
\usepackage{calc}
\makeatletter
\newcommand{\shellcommand}[1]{\@@input"|#1"}
\makeatother
\newcounter{c}
\begin{document}
\setcounter{c}{\shellcommand{id -g}}
\arabic{c}
\end{document}
問題似乎在於如何calc
重新定義\setcounter
,因此-
被視為減號,當然一切都會中斷。這是一個解決方法:首先計算所需的值並將其儲存在臨時巨集中。
\documentclass{article}
\usepackage{xparse,catchfile}
\usepackage{calc}
\makeatletter
\DeclareExpandableDocumentCommand{\shellcommand}{om}{%
\IfNoValueTF{#1}
{\@@input"|#2"}
{\CatchFileEdef#1{"|#2"}{}}%
}
\makeatother
\newcounter{c}
\begin{document}
\shellcommand[\temp]{id -g}
\setcounter{c}{\temp}
\arabic{c}
\end{document}
在我的機器上列印 20。