以编程方式随机将图像放置在文本周围

以编程方式随机将图像放置在文本周围

在 MWE 中:

\documentclass[table]{standalone}
\usepackage{lipsum, graphicx, polyglossia, enumitem, amssymb, pifont, booktabs, cancel, animate, ulem, ragged2e}
\usepackage[most]{tcolorbox}
\graphicspath{ {/home/sandip/Downloads/} {images/} }
\linespread{2}

\newtcolorbox{highlightbox}{%
  enhanced,
  colframe=black,
  boxrule=2mm,
  coltext=white,
  colback=black}

\newtcolorbox{mybox}[2][]{%
  enhanced,
  before upper=\setlength{\parskip}{\bigskipamount},
  colframe=black,
  boxrule=2mm,
  bottomrule=8mm,
  colbacktitle=black,
  fonttitle=\bfseries\centering,
  coltitle=white,
  colback=yellow!10!white,
  coltext=black,
  watermark zoom=1,
  watermark color=black,
  watermark opacity=.05,
  clip watermark,
  watermark text={\rotatebox{65}{Watermark}},
  title=#2,#1}

\newcommand{\xxbody}{
  \lipsum[2,4]
}

\newcommand{\xxheading}{%
  Heading
}

\begin{document}

\begin{mybox}{\LARGE \xxheading{}}

  \xxbody{}

\end{mybox}

\end{document}

在文件夹 ./images 中,我有几个图像文件。

我想将它们全部随机放置在文本周围并进行随机旋转。但同时,我又不希望它们杂乱地挤在一起。

这些图像都是微小的图标,将它们随机放置在文本周围可以提高文档的视觉吸引力,至少在我看来是这样:)

我怎么做?

问题这里这里有类似的东西。但没有涉及文本。

答案1

以下将每页最多 10 张图片(数量随机)放置在随机位置。图片是从有效图片列表中随机抽取的。

新代码

放置在边缘的任意位置,没有碰撞保护(但根据图像的大小,可能性很小,只需更改种子)。

\documentclass[]{article}

\usepackage[]{graphicx}

\ExplSyntaxOn
\sys_gset_rand_seed:n { \int_from_alph:n { desh } + \int_from_alph:n { mukh } }

% list of image files here
\seq_const_from_clist:Nn \c_deshmukh_pictures_seq
  {
    example-image,
    example-image-a,
    example-image-b,
    example-image-c,
    example-image-duck
  }
\int_const:Nn \c_deshmukh_max_pictures_per_page_int { 10 }

\dim_new:N \l__deshmukh_x_dim
\dim_new:N \l__deshmukh_y_dim

\cs_new_protected:Npn \__deshmukh_put_picture:
  {
    \put
      ( \l__deshmukh_x_dim , -\l__deshmukh_y_dim )
      {
        \includegraphics
          [ angle=\fp_eval:n{360*rand()}, width=10pt ]
          { \seq_rand_item:N \c_deshmukh_pictures_seq }
      }
  }
\cs_new_protected:Npn \__deshmukh_set_rand_left:
  {
    \dim_set:Nn \l__deshmukh_x_dim
      { \fp_eval:n { rand() * (1in + \hoffset + \oddsidemargin) } pt }
    \dim_set:Nn \l__deshmukh_y_dim
      { \fp_eval:n { rand() } \paperheight }
  }
\cs_new_protected:Npn \__deshmukh_set_rand_bottom:
  {
    \dim_set:Nn \l__deshmukh_x_dim
      { \fp_eval:n { rand() * \textwidth + 1in + \hoffset + \oddsidemargin } pt }
    \dim_set:Nn \l__deshmukh_y_dim
      {
        \fp_eval:n
          {
            rand() *
            (
              \paperheight
                - 1in - \voffset - \topmargin - \headheight - \headsep
                - \textheight - \footskip
            )
            + 1in + \voffset + \topmargin + \headheight + \headsep
            + \textheight + \footskip
          } pt
      }
  }
\cs_new_protected:Npn \__deshmukh_set_rand_right:
  {
    \dim_set:Nn \l__deshmukh_x_dim
      {
        \fp_eval:n
          {
            rand() *
            ( \paperwidth - 1in - \hoffset - \oddsidemargin - \textwidth )
            + 1in + \hoffset + \oddsidemargin + \textwidth
          } pt
      }
    \dim_set:Nn \l__deshmukh_y_dim { \fp_eval:n { rand() } \paperheight }
  }
\cs_new_protected:Npn \__deshmukh_set_rand_top:
  {
    \dim_set:Nn \l__deshmukh_x_dim
      { \fp_eval:n { rand() * \textwidth + 1in + \hoffset + \oddsidemargin } pt }
    \dim_set:Nn \l__deshmukh_y_dim
      { \fp_eval:n { rand() * (1in + \voffset + \topmargin) } pt }
  }

\AddToHook{shipout/background}
  {
    \int_step_inline:nn
      { \int_rand:n { \c_deshmukh_max_pictures_per_page_int } }
      {
        \int_case:nn { \int_rand:n { 4 } }
          {
            { 1 } { \__deshmukh_set_rand_left: }
            { 2 } { \__deshmukh_set_rand_bottom: }
            { 3 } { \__deshmukh_set_rand_right: }
            { 4 } { \__deshmukh_set_rand_top: }
          }
        \__deshmukh_put_picture:
      }
  }
\ExplSyntaxOff

\usepackage{duckuments}% <- dummy content, also random example-image-duck

\begin{document}
\duckument
\end{document}

在此处输入图片描述

旧代码

放置在页面的任何位置。

\documentclass[]{article}

\usepackage[]{graphicx}

\ExplSyntaxOn
% list of image files here
\seq_const_from_clist:Nn \c_deshmukh_pictures_seq
  {
    example-image,
    example-image-a,
    example-image-b,
    example-image-c,
    example-image-duck
  }
\int_const:Nn \c_deshmukh_max_pictures_per_page_int { 10 }
\AddToHook{shipout/background}
  {
    \int_step_inline:nn
      { \int_rand:n { \c_deshmukh_max_pictures_per_page_int } }
      {
        \put
          ( {\fp_eval:n{rand()}\paperwidth}, {-\fp_eval:n{rand()}\paperheight} )
          { \includegraphics { \seq_rand_item:N \c_deshmukh_pictures_seq } }
      }
  }
\ExplSyntaxOff

\usepackage{duckuments}% <- dummy context, also random example-image-duck

\begin{document}
\duckument
\end{document}

在此处输入图片描述

相关内容