
我想在我的 Latex (.tex) 檔案中建立一條註釋,如下所示:
% My pgf version is: 3.1.9a
除了版本號(上例中的“3.1.9a”)來自命令之外\pgfversion
。
有沒有辦法做到這一點?
注意:在我看來,PGF 版本最好作為註釋隱藏在 .tex 檔案中,而不是在我創建的文檔中列印出來。我的問題是,“%”註釋行起始符後面的所有文字都被字面上視為註釋文字。我在 Windows 中使用 MiKTeX,在 Ubuntu 中使用 TeX Live。答案應該是兩者相容。
答案1
如果您將下面的 Lua 腳本儲存為pgfv.lua
並有一個文件說myfile.tex
有評論
% My pgf version is: anything
然後運行
texlua pgfv.lua myfile.tex
將更新myfile.tex
以便評論包含您當前的 pgf 版本
% My pgf version is: 3.1.9a
筆記此腳本以最少的錯誤檢查重寫文件,在運行此腳本之前對其進行備份。
Lua腳本;
-- get pgf version file
kpse.set_program_name("kpsewhich")
local pgfr=kpse.lookup("pgf.revision.tex")
local f = assert(io.open(pgfr, "r"))
local content = f:read("*all")
f:close()
-- extract version string
local v,v2,pgfversion = string.find(content,"pgfversion{([^}]*)")
local myfile = io.open(arg[1], "r")
local content = myfile:read("*all")
myfile:close()
print ("% My pgf version is: " .. pgfversion)
-- edit comment
content = string.gsub(content,
"%% My pgf version is: %S*",
"%% My pgf version is: " .. pgfversion)
-- update file BEWARE WRITES FILE
local myfile = io.open(arg[1], "w")
myfile:write(content)
myfile:close()