\pgfversion でコメントを作成する方法

\pgfversion でコメントを作成する方法

次のように 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()

関連情報