Created
December 7, 2020 09:23
-
-
Save k16shikano/44a17a204e932d50f1d0c2686fa12221 to your computer and use it in GitHub Desktop.
Mecabを使って改行位置をいい感じに調整する(LuaTeX)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| % inspired by https://tech.aptpod.co.jp/entry/2020/11/20/160000 | |
| % with http://taku910.github.io/mecab/ | |
| % ref. https://tex.stackexchange.com/questions/500372/conditional-string-replacements-in-lualatex | |
| \documentclass[a4paper]{ltjsarticle} | |
| \usepackage[no-math]{luatexja-fontspec} | |
| \usepackage{luacode,luatexbase} | |
| %\usepackage[callback={preline},verbosity=1]{nodetree} | |
| %\usepackage{lipsum} | |
| \usepackage[textwidth=32em,textheight=5cm,paperwidth=32em,paperheight=5cm]{geometry} | |
| \title{\wakatiBreak{とても長いタイトルを持ち秋の夜に世界の片隅で作成されたPDF出力テストのためのドキュメント}} | |
| %\stitle{とても長いタイトルを持ち秋の夜に世界の片隅で作成されたPDF出力テストのためのドキュメント} | |
| \begin{document} | |
| \begingroup | |
| \ttfamily | |
| \let\\\relax | |
| \catcode`\^^M=12 | |
| \catcode`\%=12 | |
| \luaexec{ | |
| mecab = require("lua-mecab") | |
| parser = mecab:new("-Owakati") | |
| function break_at_wakachi (s) | |
| if s ~= "\\wakatiBreakOff" then | |
| local t = {} | |
| split_tex (t, s) | |
| return table.concat (t) | |
| end | |
| end | |
| function split_tex (t, s, is_cmd) | |
| if s == nil then | |
| return t | |
| else | |
| local c = string.sub (s, 0, 1) | |
| if c == '\\' then | |
| found, rest = string_find (s, "\\[%a]+") | |
| if not found then | |
| found, rest = string_find (s, "\\[^%a]") | |
| end | |
| table.insert(t, found) | |
| split_tex (t, rest, true) | |
| elseif c == '[' then | |
| if is_cmd then | |
| found, rest = string_find (s, "[^\\]]") | |
| table.insert(t, found) | |
| split_tex (t, rest, false) | |
| else | |
| table.insert(t, "[") | |
| split_tex (t, string.sub(s, 1, -1), 0) | |
| end | |
| else | |
| found, rest = string_find (s, "[^\\\\]+") | |
| if found ~= nil then | |
| table.insert(t, setBreak(parser:parse(found))) | |
| end | |
| split_tex (t, rest, false) | |
| end | |
| end | |
| end | |
| function string_find (s, pattern) | |
| found = string.match (s, pattern) | |
| if found ~= nil then | |
| rest = string.sub (s, string.len(found) + 1, -1) | |
| if rest == "" then | |
| rest = nil | |
| end | |
| return found, rest | |
| else | |
| return nil | |
| end | |
| end | |
| function setBreak (s) | |
| f, r = string_find (s, "[^%s]+") | |
| if f == nil then | |
| s = "" | |
| elseif r == nil then | |
| s = f | |
| elseif string.match(string.sub(string.reverse(f), 1, 1), "%w") | |
| and string.match(string.sub(r, 2, 2), "%w") then | |
| s = f .. " " .. setBreak(string.sub(r, 2, -1)) | |
| else | |
| print (f) | |
| print (r) | |
| s = f .. "\\penalty -5{}" .. setBreak(string.sub(r, 2, -1)) | |
| end | |
| return s | |
| end | |
| } | |
| \endgroup | |
| \newcommand\wakatiBreakOn{\directlua{luatexbase.add_to_callback("process_input_buffer", break_at_wakachi, "break_at_wakachi" )}} | |
| \newcommand\wakatiBreakOff{\directlua{luatexbase.remove_from_callback("process_input_buffer", "break_at_wakachi" )}} | |
| \newcommand\wakatiBreak[1]{\directlua{tex.sprint(break_at_wakachi(\luastringN{#1}))}} | |
| \wakatiBreakOn | |
| \maketitle | |
| あなたとJAVA, 今すぐダウンロード. | |
| \wakatiBreakOff | |
| %\directlua{tex.print(parser:parse("あなたとJAVA, 今すぐダウンロード. "))} | |
| \end{document} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment