如何创建不间断的任意长度蛇形以实现全宽对齐?

如何创建不间断的任意长度蛇形以实现全宽对齐?

这是最近韓國漫画:

在此处输入图片描述

这是一个简单的版本,没有任何理由:

\documentclass{article}
\usepackage[text={1.2in,3in}]{geometry}
\raggedright
\begin{document}
\noindent Their famous paper on the relationship between deindustrialization and the growth of ecological \ldots
\end{document}

在此处输入图片描述

如何在 LaTeX 中制作蛇?为了使问题更具体,我如何让 LaTeX 自动为未到达列尾的每一行添加长下划线,然后添加字母“s”?我假设有了这些知识,我就能创建蛇的身体和头部。

答案1

这只是 lualatex,它绘制了一条 PDF 线w点,以填充每条线,如果你有艺术感,可以w 0 l用绘制相同长度的蛇的东西来代替

在此处输入图片描述

\documentclass{article}

\directlua{
PDF_LITERAL = node.subtype("pdf_literal")
WS=node.id("whatsit")
GLUE  = node.id("glue")
snakefill=function(head)
  for line in node.traverse_id(0,head) do
  for n in node.traverse_id(GLUE,line.head) do
  if n.subtype==9 then
  print('  glue: ' .. n.subtype .. ' ' ..node.effective_glue(n,line) )
  local w = node.effective_glue(n,line) / 65536
local sn = node.new(WS,PDF_LITERAL)
sn.data="0 G 0 0 m  " .. w .. " 0 l S 0 g"
  line=node.insert_before(line,n,sn)
    end
   end
  end
return head
end
}
\def\snakefill{%
\directlua{
luatexbase.add_to_callback("post_linebreak_filter",snakefill,"snake fill")
}%
}
\def\offsnakefill{%
\directlua{
luatexbase.remove_from_callback("post_linebreak_filter","snake fill")
}%
}

\begin{document}

\raggedright\parfillskip=0pt
\snakefill
one two three four five
one two three four five
one two three four fivexxxxxxxxxxxx
one two three four five
one two three four five
one two three four five
one two three four five
one two three four five
one two three four five
one two three four five
one two three four five
one two three four five
one two three four five
\end{document}

或者更像蛇的版本

在此处输入图片描述

% http://images.clipartpanda.com/cute-snake-clipart-black-and-white-30511wall.png

\directlua{
GLUE  = node.id("glue")
snakefill=function(head)
  for line in node.traverse_id(0,head) do
  for n in node.traverse_id(GLUE,line.head) do
  if n.subtype==9 then
  print('  glue: ' .. n.subtype .. ' ' ..node.effective_glue(n,line) )
  local w = node.effective_glue(n,line)
local sni = img.node{filename="cute-snake-clipart-black-and-white-30511wall.png"}
sni.height=500000
sni.width=w
  line=node.insert_before(line,n,sni)
    end
   end
  end
return head
end
}

答案2

确实不是蛇,但是……

在此处输入图片描述

\documentclass{article}
\usepackage[text={1.2in,3in}]{geometry}
\def\snake{\cleaders\hbox to .5em{\hss\ensuremath\sim\hss%
\hspace{-.3em minus -.2em}%
}\hfill$^{_\diamond}$\break}
\raggedright
\begin{document}
\noindent Their famous paper on the relationship between \snake
deindustrialization and \snake the growth of the main ecological  \snake
\end{document}

答案3

虽然我无法得到大卫·卡莱尔的回答为了工作,我制作了一个简单的可绑定矢量版本的 xkcd 蛇:

结果

\documentclass{article}
\usepackage[text={1.2in,3in}]{geometry}
\usepackage[none]{hyphenat}

\directlua{
    PDF_LITERAL = node.subtype("pdf_literal")
    WS = node.id("whatsit")
    GLUE = node.id("glue")
    add_image = function(line, n, width, height, name)
        local sni = img.node{filename=name}
        sni.height = height
        sni.width = width
        return node.insert_before(line,n,sni)
    end
    add_snake = function(line, n, width, height)
        local segments_middle = 0;
        local segments_wave = 0;
        local unstretched_width = height * (1.0 + 1.5)

        while unstretched_width + height * 1.5 < width do
            unstretched_width = unstretched_width + height * 1.5
            segments_wave = segments_wave + 1
        end

        while unstretched_width + height * 0.5 < width do
            unstretched_width = unstretched_width + height * 0.5
            segments_middle = segments_middle + 1
        end

        local stretch = width / unstretched_width

        line = add_image(line, n, height * 1.0 * stretch, height, "snake_tail.pdf")

        for i = 1, segments_wave do
            line = add_image(line, n, height * 1.5 * stretch, height, "snake_wave.pdf")
        end

        for i = 1, segments_middle do
            line = add_image(line, n, height * 0.5 * stretch, height, "snake_middle.pdf")
        end

        line = add_image(line, n, height * 1.5 * stretch, height, "snake_head.pdf")

        return line
    end
    local w = 1250000 % remove when effective_glue works!
    snakefill = function(head)
        for line in node.traverse_id(0,head) do
            for n in node.traverse_id(GLUE,line.head) do
                if n.subtype == 9 then
                    %local w = node.effective_glue(n,line) / 65536
                    w = w * 1.1 % remove when effective_glue works!
                    local h = 500000
                    line = add_snake(line, n, w, 500000)
                end
            end
        end
        return head
    end
}
\def\snakefill{%
    \directlua{
        luatexbase.add_to_callback("post_linebreak_filter",snakefill,"snake fill")
    }%
}
\def\offsnakefill{%
    \directlua{
        luatexbase.remove_from_callback("post_linebreak_filter","snake fill")
    }%
}

\begin{document}
    \raggedright
    \snakefill
    \noindent Their famous paper on the relationship between deindustrialization and the growth of ecological\ldots\par
    \noindent Their famous paper on the relationship between deindustrialization and the growth of ecological\ldots\par
    \noindent Their famous paper on the relationship between deindustrialization and the growth of ecological\ldots\par
\end{document}

蛇的图像可以从这个要点effective_glue。如果我找到上班的方法,我会更新帖子。

答案4

蛇可以改进。另外,我不知道如何使用 tikz 坐标进行计算,所以我改用 zref。

编辑

实际上,我认为我的第一个示例并非在所有情况下都有效,因为即使开始和结束标记在同一行上,它们也不需要位于同一位置。因此,最好测试第二个标记是否位于行尾。

一些解释:与其他答案不同,我的文本不使用 \raggedright,但完全对齐。目前文本必须是一页,但由于 tikzmark 存储了页码,因此可以添加页面测试。必须在任何地方添加“\optsnake”,但这可以通过回调使用 luatex 来完成。

\documentclass{article}
\textwidth=2.9cm
\usepackage[none]{hyphenat}
\usepackage{tikz}
\usepackage{zref-savepos}
\usetikzlibrary{tikzmark}
\usetikzlibrary{decorations.pathmorphing}



\newcounter{snakefill}
\newcommand\optsnake{%
 \stepcounter{snakefill}%
 \pgfmark{snakemarkbegin\the\value{snakefill}}%
 \zsavepos{snakemarkbegin\the\value{snakefill}}%
 \hfil
 \zsavepos{snakemarkend\the\value{snakefill}}%
 \pgfmark{snakemarkend\the\value{snakefill}}%
 \penalty50
 \hfilneg
 }

\newcommand\printsnakes{%
  \begin{tikzpicture}[overlay,remember picture]
  \foreach \x in {1,...,\the\value{snakefill}}
  {
   \ifnum  \zposx{snakemarkend\x}=\zposx{snakeendofline}
    \draw [->,decorate,decoration=snake] (pic cs:snakemarkbegin\x) -- (pic cs:snakemarkend\x);
   \fi
  }
  \end{tikzpicture}}
\begin{document}
\mbox{}\hfill\zsavepos{snakeendofline}% get the end of line value

\noindent
on the \optsnake relationship \optsnake
between \optsnake
Deindustrialization \optsnake
on the \optsnake relationship \optsnake
between \optsnake
Deindustrialization \optsnake
on the blblb \optsnake relationship \optsnake
between \optsnake
Deindustrialization \optsnake


\printsnakes

\end{document}

在此处输入图片描述

相关内容