(来源:bankfotek.pl)
我有章节,在每一行的末尾我会写上字符的计数器,并在文本行之前写一行字符数的大小。
请使用 LuaTeX 解决方案。我的 LaTeX 代码
\begin{document}
\begin{minipage}{5cm}
\blindtext
\end{minipage}
\end{document}
答案1
这是一个概念证明:
\documentclass{article}
\usepackage{luacode,luatexbase}
\begin{document}
\begin{luacode*}
function countchars(head)
while head do
if head.id == 0 then
-- a hbox
local c = 0
local first_font
for n in node.traverse_id(37,head.head) do
c = c + 1
first_font = first_font or n.font
end
local glue = node.new("glue")
glue.spec = node.new("glue_spec")
glue.spec.stretch = 2^16
glue.spec.stretch_order = 3
local rule = node.new("rule")
rule.width = 1 * c * 2^16
rule.height = 2 * 2^16
while c > 0 do
local digit = math.mod(c,10)
local glyf = node.new("glyph")
glyf.font = first_font
glyf.char = 48 + digit
node.insert_after(glue,glue,glyf)
c = c - digit
c = c / 10
end
local llap_glue = node.new("glue")
llap_glue.spec = node.new("glue_spec")
llap_glue.spec.stretch = 2^16
llap_glue.spec.stretch_order = 2
llap_glue.spec.shrink = 2^16
llap_glue.spec.shrink_order = 2
node.insert_after(llap_glue, llap_glue, rule)
llap_hbox = node.hpack(llap_glue,0,"exactly")
local marker = node.insert_before(glue,glue,llap_hbox)
local h = node.hpack(marker,head.width + 14*2^16,"exactly")
h.width = 0
head.head = node.insert_before(head.head,head.head,h)
end
head = head.next
end
return true
end
luatexbase.add_to_callback("post_linebreak_filter",countchars,"countchars")
\end{luacode*}
A \emph{wonderful} serenity has taken {\large possession} of my entire soul, like these
\textsl{sweet}
\textbf{mornings} of spring which I enjoy with my whole heart. I am alone, and feel the
charm of existence in this spot, \textbf{which} was created for the bliss of souls like
mine. I am so happy, my dear friend, so absorbed in the exquisite sense of
mere tranquil existence, that I neglect my talents. I should be incapable of
drawing a single stroke at the present moment; and yet I feel that I never was
a greater artist than now.
\end{document}