裁剪(而不是缩放)背景图像并生成图像而不是 PDF

裁剪(而不是缩放)背景图像并生成图像而不是 PDF

这是我的 MWE:

\documentclass{standalone}
\usepackage{graphicx, lipsum}
\usepackage[most]{tcolorbox}

\newtcolorbox{myminipage}[3][]{
    breakable,
    blankest, 
    watermark graphics=#3, 
    watermark stretch=1,
    width=#2,
    #1
}

\begin{document}

\begin{myminipage}{3cm}{example-image}
\lipsum[2]
\end{myminipage}

\end{document}

这取自这里

背景图像会缩放。可以将其裁剪吗?

最后,是否可以直接生成 jpeg 或 png ?

答案1

您正在寻找有standalone该选项的课程varwidth吗?

\documentclass[varwidth]{standalone}
\usepackage{ eso-pic,tikz, lipsum}
\usetikzlibrary{tikzmark}
\linespread{2}
\newcommand\BackgroundPic{%
\begin{tikzpicture}[overlay,remember picture]
\node[anchor=north west,inner sep=0pt] at ([yshift=8pt]pic cs:start) {%
\includegraphics[width=\textwidth]{example-image}};
\end{tikzpicture}}

\begin{document}
\AddToShipoutPicture*{\BackgroundPic}

\tikzmark{start}\lipsum[1-1]

\end{document}

在此处输入图片描述

旧版本tikzpagenodes允许您精确地确定文本区域。

\documentclass{article}
\usepackage{geometry, eso-pic, tikzpagenodes, lipsum}
\linespread{2}
\newcommand\BackgroundPic{%
\begin{tikzpicture}[overlay,remember picture]
\node[anchor=north west,inner sep=0pt] at (current page text area.north west) {
\includegraphics[width=\textwidth,height=\textheight,%
]{example-image}};
\end{tikzpicture}}
\geometry{
  a4paper, 
  portrait, 
  margin=1in, 
  top=.25in, 
  bottom=1.75in
}

\begin{document}
\AddToShipoutPicture*{\BackgroundPic}
\thispagestyle{empty}

\lipsum[1-1]

\end{document}

在此处输入图片描述

这是根据文本裁剪图像的方法......

\documentclass{article}
\usepackage{geometry, eso-pic, tikzpagenodes, lipsum}
\usetikzlibrary{tikzmark}
\linespread{2}
\newcommand\BackgroundPic{%
\begin{tikzpicture}[overlay,remember picture]
\path ([yshift=12pt]pic cs:start) coordinate(aux1) ([yshift=-3pt]pic cs:end) coordinate(aux2);
\clip (current page text area.west |-aux1) rectangle
(current page text area.east |-aux2);
\node[anchor=north west,inner sep=0pt] at (current page text area.north west) {%
\includegraphics[width=\textwidth]{example-image}};
\end{tikzpicture}}
\geometry{
  a4paper, 
  portrait, 
  margin=1in, 
  top=.25in, 
  bottom=1.75in
}

\begin{document}
\AddToShipoutPicture*{\BackgroundPic}
\thispagestyle{empty}

\tikzmark{start}\lipsum[1-1]\tikzmark{end}

\end{document}

在此处输入图片描述

答案2

如果包含的图像包含在覆盖或底层tcbclipframe环境中而不是作为水印,您仍然可以使用tcolorbox

\documentclass[tikz]{standalone}
\usepackage{graphicx, lipsum}
\usepackage[most]{tcolorbox}

\newtcolorbox{myminipage}[3][]{
     enhanced,
    blankest, 
    width=#2,
    underlay={\begin{tcbclipframe}
    \node at (frame) {\includegraphics{#3}};
    \end{tcbclipframe}},
    #1
}

\begin{document}

\begin{myminipage}{3cm}{example-image}
This is some text not so long like \texttt{\textbackslash{}lipsum[2]}
\end{myminipage}

\begin{myminipage}{10cm}{example-image}
\lipsum[2]
\end{myminipage}
\end{document}

在此处输入图片描述

关于获取 .jpg 或 .png 结果,这个答案可以帮你。

相关内容