我有一个名为 20160201.tex 的 .tex 文件,我想从该文件中提取不带 .tex 的文件名,并将其转换为字符串 February 01, 2016? 我可以通过以下代码获取该字符串,但是,我找不到进行转换的简单方法。
\documentclass{article}
\usepackage{currfile}
\begin{document}
This file was created on \currfilebase
\end{document}
答案1
您可以使用具有 8 个参数的宏,每个参数都是文件名中的一个字符,然后用于\ifcase
打印月份名称:
\documentclass{article}
\usepackage{currfile}
\newcommand\parsedate[1]{%
\expandafter\parsedateaux#1\relax}
\def\parsedateaux#1#2#3#4#5#6#7#8#9\relax{%
\ifcase#5#6 \or%Y Y Y Y M M D D
January\or February\or March\or April\or May\or June\or July\or
August\or September\or October\or November\or December\fi
\space#7#8, #1#2#3#4}
\begin{document}
This file was created on \parsedate{\currfilebase}
\end{document}
答案2
我猜总有 Lua:
% TeX Program = lualatex
\documentclass{article}
\usepackage{currfile}
\usepackage{luacode}
\begin{luacode}
function formatdate(isodate)
local y, m, d = string.match(isodate, "(%d%d%d%d)(%d%d)(%d%d)")
tex.sprint(os.date("%B %d, %Y", os.time({year=y, month=m, day=d})))
end
\end{luacode}
\newcommand*{\filedate}[1][\currfilebase]{\directlua{formatdate(#1)}}
\begin{document}
This file was created on \filedate.
\end{document}
答案3
我认为我找到了使用xstring
和datetime
包的解决方案
\documentclass{article}
\usepackage{currfile}
\usepackage{xstring}
\usepackage{datetime}
\newcommand{\numdate}{%
\StrLeft{\currfilebase}{4}[\yearstr]%
\StrRight{\currfilebase}{2}[\daystr]%
\StrMid{\currfilebase}{5}{6}[\monthstr]%
\monthname[\monthstr] \daystr, \yearstr}
\begin{document}
This file was created on \numdate
\end{document}