Skip to content

Instantly share code, notes, and snippets.

@zr-tex8r
Created December 17, 2015 14:27
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save zr-tex8r/cf7bf7d04d18304e25a6 to your computer and use it in GitHub Desktop.
Save zr-tex8r/cf7bf7d04d18304e25a6 to your computer and use it in GitHub Desktop.
LaTeX: zrefパッケージを利用して「最後のコマンド」を実装する
\RequirePackage{zref}
%% prepare zref list
\zref@newlist{LastDo}
\AtEndDocument{\zref@labelbylist{lastdo:}{LastDo}}
%<+> \NewLastDoCommand\命令{<非最後>}{<最後>}
\newcommand{\NewLastDoCommand}[3]{%
\@ifdefinable{#1}{% \命令 が定義可能なら
% 命令名を抽出して \lastdo@new@a に進む
\edef\next{\noexpand\lastdo@new@a
{\expandafter\@gobble\string#1}}%
\next#1{#2}{#3}}}
%% \lastdo@new@a{<命令名>}\命令{<非最後>}{<最後>}
\def\lastdo@new@a#1#2#3#4{%
% プロパティ 'lastdo@命令名' を作成する
\zref@newprop{lastdo@#1}[0]{0}%
\zref@addprop{LastDo}{lastdo@#1}%
% 命令を定義する
\gdef#2{\lastdo@do{#1}{#3}{#4}}}
%% \lastdo@do{<命令名>}{<非最後>}{<最後>}
% LastDo命令の本体.
\def\lastdo@do#1#2#3{%
% '現在値'を読み込む
\@tempcnta=\zref@getcurrent{lastdo@#1}%
% '現在値'をインクリメントして書き込む
\advance\@tempcnta 1
\edef\next{\noexpand\zref@setcurrent{lastdo@#1}{\the\@tempcnta}%
}\next
% '現在値'が'前回の値'とを比較する
\ifnum\zref@extract{lastdo:}{lastdo@#1}=\@tempcnta #3%
\else#2\fi}
\documentclass[a4paper]{article}
\usepackage{lastdo}
\NewLastDoCommand\hoge{Hoge}{LastHoge}
\NewLastDoCommand\fuga{Fuga}{LastFuga}
\begin{document}
\hoge, \fuga, \hoge, \hoge! \fuga!!
\end{document}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment