我的示例在这个位置不起作用string.find(s:sub(1, 1), "%a")
。
如何修复?
梅威瑟:
% !TeX program = lualatex
% !TeX encoding = utf-8
%============================================================================================
\documentclass[12pt, a4paper]{article}
\usepackage{polyglossia}
\setdefaultlanguage[spelling=modern]{russian}
\setotherlanguage{english}
\defaultfontfeatures{Ligatures={TeX},Renderer=Basic}
\setmainfont{Times New Roman}
\setsansfont{Arial}
\setmonofont{Courier New}
\newfontfamily\cyrillicfonttt[Script=Cyrillic]{Courier New}
%==========================================================
\usepackage[tracking=true]{microtype}
\microtypecontext{kerning=russian}
\usepackage{luacode}
%%%%%%%%%%%%%%%%
\def\addtosort#1#2{%
\directlua{%
tbl=tbl or {}%
table.insert(tbl, {abbr="\luaescapestring{#1}", desc="\luaescapestring{#2}"})%
}%
}
\newcommand{\key}[2]{#1\addtosort{#1}{#2}}
%%%%%%%%%%%%%%%%
\def\printsorted{%
\begingroup
\catcode`\%=12
\directlua{%
tblru=tbl or {}
local function ChangingLatinStr(s)
if string.find(s:sub(1, 1), "%a") then
return "ЯЯЯ" .. s
else return s
end
end
table.sort(tbl, function (a,b) return (ChangingLatinStr(a.abbr) < ChangingLatinStr(b.abbr)) end)
for i,n in ipairs(tbl) do
tex.print("\string\\printkw{" .. n.abbr .. "}{" .. n.desc .. "}")
end
}%
\endgroup
}
\newcommand\printkw[2]{\textbf{#1}~-- #2.\\}
\newcommand{\printkey}{%
\typeout{KW: NOW SHOWING}
\noindent\rule{\textwidth}{3pt}
\noindent\printsorted
}
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\begin{document}
\key{ЯНАО}{Ямало-ненецкий автономный округ}.
\key{LED}{Light emitting diode}.
\key{FET}{Field effect transistor}.
\key{БД}{База данных}.
\key{ЛПР}{Лицо, принимающее решения}.
\key{XML}{Extensible markup language}.
\printkey
\end{document}
答案1
与 lua 无关,您需要在定义之外更改 catcode,在内部更改为时已晚,因为 % 已被视为注释。
另外,由于%
没有做出任何评论,您不能使用它来注释掉行尾。
\documentclass[12pt, a4paper]{article}
\usepackage{polyglossia}
\setdefaultlanguage[spelling=modern]{russian}
\setotherlanguage{english}
\defaultfontfeatures{Ligatures={TeX},Renderer=Basic}
\setmainfont{Times New Roman}
\setsansfont{Arial}
\setmonofont{Courier New}
\newfontfamily\cyrillicfonttt[Script=Cyrillic]{Courier New}
%==========================================================
\usepackage[tracking=true]{microtype}
\microtypecontext{kerning=russian}
\usepackage{luacode}
%%%%%%%%%%%%%%%%
\def\addtosort#1#2{%
\directlua{%
tbl=tbl or {}%
table.insert(tbl, {abbr="\luaescapestring{#1}", desc="\luaescapestring{#2}"})%
}%
}
\newcommand{\key}[2]{#1\addtosort{#1}{#2}}
%%%%%%%%%%%%%%%%
\begingroup
\catcode`\%=12
\gdef\printsorted{\directlua{
tblru=tbl or {}
local function ChangingLatinStr(s)
if string.find(s:sub(1, 1), "%a") then
return "ЯЯЯ" .. s
else return s
end
end
table.sort(tbl, function (a,b) return (ChangingLatinStr(a.abbr) < ChangingLatinStr(b.abbr)) end)
for i,n in ipairs(tbl) do
tex.print("\string\\printkw{" .. n.abbr .. "}{" .. n.desc .. "}")
end
}
}
\endgroup
\newcommand\printkw[2]{\textbf{#1}~-- #2.\\}
\newcommand{\printkey}{%
\typeout{KW: NOW SHOWING}
\noindent\rule{\textwidth}{3pt}
\noindent\printsorted
}
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\begin{document}
\key{ЯНАО}{Ямало-ненецкий автономный округ}.
\key{LED}{Light emitting diode}.
\key{FET}{Field effect transistor}.
\key{БД}{База данных}.
\key{ЛПР}{Лицо, принимающее решения}.
\key{XML}{Extensible markup language}.
\printkey
\end{document}