后续问题

后续问题

我想在左对齐排版段落的每一行开头添加一个随机空格。这个想法是为了模拟手写手稿。

我以为我可以使用 trick lineno 包来做到这一点,但这不是逻辑。欢迎任何想法。


后续问题

Sašo Živanović 的解决方案有效,但在类似的背景下,它与之前定义的宏相冲突:段落形状保持正常。

% Based on some ideas from http://tex.stackexchange.com/a/29458
\documentclass{article}

% thanks to Bruno Le Floch
\usepackage{rotating}
\usepackage[first=-3,last=5]{lcg}% you can play around with these values
\makeatletter
\newcommand{\globalrand}{\rand\global\cr@nd\cr@nd}
\makeatother
\newcommand{\randomrotation}[1]{\globalrand\turnbox{\value{rand}}{#1}\phantom{#1‌​}}
% thanks...
\makeatletter
\def\handmove#1{%
  \@handmove#1 \@empty }
\def\@handmove#1 #2{%
  \randomrotation{#1}\space
  \ifx #2\@empty
  \else
    \expandafter\@handmove
  \fi
  #2%
}
\makeatother

%la macro de Saso Zivanovic
\usepackage{pgf,pgffor}
\usepackage{etoolbox}

\def\randomflushleftpercent{0.3}
\def\randomflushleftn{42}

\newenvironment{randomflushleft}{%
  \raggedright
  \everypar={\computerandomparshape}
}{}

\def\computerandomparshape{%
  \gdef\myparshape{\parshape\randomflushleftn\space}%
  \foreach \n in {1,2,...,\randomflushleftn} {%
    \pgfmathparse{rnd*\randomflushleftpercent*\textwidth}%
    \xappto\myparshape{\pgfmathresult pt }%
    \pgfmathparse{\textwidth-\pgfmathresult}%
    \xappto\myparshape{\pgfmathresult pt }%
  }%
  \myparshape
}
\begin{document}
\pagestyle{empty}
\begin{randomflushleft}
  \handmove{Solacia dua, nequaquam paria tanto dolori, solacia
    tamen. Solacia dua, nequaquam paria tanto dolori, solacia
    tamen. Solacia dua, nequaquam paria tanto dolori, solacia
    tamen.}
\end{randomflushleft}
\end{document}

错误结果

答案1

正如大卫所建议的...随机生成\parshape

42 应该足够大。\randomflushleftpercent确定左侧空白的最大量(以文本宽度的百分比表示)。

\documentclass{article}
\usepackage{lipsum}

\usepackage{pgf,pgffor}
\usepackage{etoolbox}

\def\randomflushleftpercent{0.1}
\def\randomflushleftn{42}

\newenvironment{randomflushleft}{%
  \raggedright
  \everypar={\computerandomparshape}
}{}

\def\computerandomparshape{%
  \gdef\myparshape{\parshape\randomflushleftn\space}%
  \foreach \n in {1,2,...,\randomflushleftn} {%
    \pgfmathparse{rnd*\randomflushleftpercent*\textwidth}%
    \xappto\myparshape{\pgfmathresult pt }%
    \pgfmathparse{\textwidth-\pgfmathresult}%
    \xappto\myparshape{\pgfmathresult pt }%
  }%
  \myparshape
}

\begin{document}
\parskip 1ex

\begin{randomflushleft}
  \lipsum
\end{randomflushleft}

\end{document}

回答后续问题

\parshape在-setting within\everypar和 引入的组之间存在(不幸的)交互\handmove\everypar标记在段落进入水平模式之前插入。当\handmove{...}是段落中的第一件事时,\everypar因此在由 开启的组中调用\turnbox。该组在段落结束前关闭,导致集合\parshape被遗忘。解决方案:\handmove以 开头\mbox{}

环境末尾也出现了类似情况randomflushleft。段落尚未结束,但由 引入的组randomflushleft已关闭。解决方案:randomflushleft以显式结尾\par

\documentclass[draft]{article}
\usepackage[utf8x]{inputenc}
\usepackage{lipsum}

% thanks to Bruno Le Floch
\usepackage{rotating}
\usepackage[first=-3,last=5]{lcg}% you can play around with these values
\makeatletter
\newcommand{\globalrand}{\rand\global\cr@nd\cr@nd}
\makeatother
\newcommand{\randomrotation}[1]{\globalrand\turnbox{\value{rand}}{#1}\phantom{#1‌​}}
% thanks...
\makeatletter
\def\handmove#1{%
  \mbox{}\@handmove#1 \@empty }
\def\@handmove#1 #2{%
  \randomrotation{#1}\space
  \ifx #2\@empty
  \else
    \expandafter\@handmove
  \fi
  #2%
}
\makeatother

%la macro de Saso Zivanovic
\usepackage{pgf,pgffor}
\usepackage{etoolbox}

\def\randomflushleftpercent{0.3}
\def\randomflushleftn{42}

\makeatletter
\newenvironment{randomflushleft}{%
  \everypar={\randomparshape}%
  \raggedright
  \rightskip \z@ plus2em
}{\par}
\makeatother

\def\randomparshape{%
  \xdef\myparshape{\noexpand\parshape\randomflushleftn\space}%
  \foreach \n in {1,2,...,\randomflushleftn} {%
    \pgfmathparse{rnd*\randomflushleftpercent*\textwidth}%
    \xappto\myparshape{\pgfmathresult pt
      \the\dimexpr\textwidth-\pgfmathresult pt\relax\space}%
  }%
  \myparshape
}
\begin{document}
\pagestyle{empty}
\parskip 2ex

\begin{randomflushleft}
  \handmove{Solacia dua, nequaquam paria tanto dolori, solacia
    tamen. Solacia dua, nequaquam paria tanto dolori, solacia
    tamen. Solacia dua, nequaquam paria tanto dolori, solacia
    tamen.}
\end{randomflushleft}

\end{document}

正确的结果

代码中还有一些进一步的更改,大多数只是美化,但其中一项非常重要:如果没有 的\rightskip \z@ plus2em定义randomflushleft,第一行可能会结束得太早。我发现这是一个非常有趣的现象:我发布了一个问题关于它。

答案2

在此处输入图片描述

\documentclass{article}

\textwidth.3\textwidth

\begin{document}


\def\a{One two three four five six seven eight nine ten. }
\def\b{\a\a\a  Red green blue yellow white black. \a}

\def\p#1{#1 \dimexpr\textwidth-#1\relax}
\raggedright
\itshape
\parshape 20 
\p{1pt}\p{3pt}\p{1pt}\p{2pt}\p{3pt}
\p{2pt}\p{1pt}\p{4pt}\p{1pt}\p{3pt}
\p{1pt}\p{3pt}\p{5pt}\p{2pt}\p{1pt}
\p{3pt}\p{2pt}\p{3pt}\p{1pt}\p{2pt}
\b

\end{document}

这里我只是编造了偏移量的“随机”列表,但你可以根据需要使用随机数生成器(来自 pgfmath 或您选择的任何数字)来生成该列表

相关内容