如何增加小页面相对于其中文本的水平边距?

如何增加小页面相对于其中文本的水平边距?

我遇到了以下问题:我想在以给定图像作为背景的迷你页面中写一些文本。这是通过下面的代码完成的,它工作正常,但我想对图像的边框有更多的控制。我希望图像比文本更宽(比现在更宽),但我想保持它相对于文本的高度与现在一样。

如果您看到已定义新环境,则当它调用 includegraphics 函数时,它将宽度定义为 width=\dimexpr\wd\mysavebox。我尝试对其进行修改,并且可以放大图像宽度,但问题是它只会增加右边距。图像左边距和文本开头之间的距离仍然相同。

您能否给我一些建议,关于如何将文本放置在小页面的中心,同时增加水平边距的大小?

谢谢。

\setlength{\fboxrule}{0pt}

\newsavebox\mysavebox
\newenvironment{imgminipage}[2][]{\def\imgcmd{\includegraphics[width=\dimexpr\wd\mysavebox,height=\dimexpr\ht\mysavebox+\dp\mysavebox\relax,#1]{#2}}%
\begin{lrbox}{\mysavebox}%
\begin{minipage}[c][][c]{1\textwidth}}{
\end{minipage}
\end{lrbox}%
\sbox\mysavebox{\fbox{\usebox\mysavebox}}%
\mbox{\rlap{\raisebox{-\dp\mysavebox}{\imgcmd}}\usebox\mysavebox}%
}

这是一个有效的例子

\documentclass[]{paper}
\usepackage{graphicx}
\setlength{\fboxrule}{0pt}

\newsavebox\mysavebox
\newenvironment{imgminipage}[2][]{\def\imgcmd{\includegraphics[width=\dimexpr\wd\mysavebox+\dp\mysavebox\relax,height=\dimexpr\ht\mysavebox+\dp\mysavebox\relax,#1]{#2}}%
\begin{lrbox}{\mysavebox}%
\begin{minipage}[c][][c]{1\textwidth}}{
\end{minipage}
\end{lrbox}%
\sbox\mysavebox{\fbox{\usebox\mysavebox}}%
\mbox{\rlap{\raisebox{-\dp\mysavebox}{\imgcmd}}\usebox\mysavebox}% 
}

\begin{document}

\begin{center}
\hspace*{-0.5cm}
\begin{imgminipage}{bg}
\vspace{0.2cm} 
text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text
 \vspace{0.2cm}

\end{imgminipage}
\vspace{-0.5cm}
\end{center}

\end{document}

我把宽度参数增加了与高度相同的量,这样你就能看到发生了什么。如果我删除我添加的内容,文本会居中,但边距会很近。

在此处输入图片描述

答案1

要更改所有边距,只需使用\fboxsep(和前导\noindent\hspace{}):

\documentclass{article}
\usepackage{graphicx,lipsum}
\begin{document}
\setlength{\fboxrule}{0pt}
\fboxsep=20pt

\newsavebox\mysavebox
\newenvironment{imgminipage}[2][]{\noindent\hspace{-\fboxsep}%
  \def\imgcmd{\includegraphics[width=\dimexpr\wd\mysavebox,%
   height=\dimexpr\ht\mysavebox+\dp\mysavebox\relax,#1]{#2}}%
\begin{lrbox}{\mysavebox}%
\begin{minipage}[c][][c]{1\textwidth}}{
\end{minipage}
\end{lrbox}%
\sbox\mysavebox{\fbox{\usebox\mysavebox}}%
\mbox{\rlap{\raisebox{-\dp\mysavebox}{\imgcmd}}\usebox\mysavebox}%
}
\begin{imgminipage}{example-image}
blah blah blah
blah blah blah
blah blah blah
blah blah blah
blah blah blah
blah blah blah
blah blah blah
blah blah blah
blah blah blah
blah blah blah
blah blah blah
blah blah blah
blah blah blah
\end{imgminipage}
\lipsum[1]
\end{document}

在此处输入图片描述

为了独立设置左/右和上/下边距,我使用\fboxsep表示左/右边距,以及\tbmargin表示上/下边距。为了实现这一点,我修改了您的两个计算:height=的参数\includegraphics和 的参数。我还在环境的开头\raisebox添加了以正确设置水平位置,并在结尾添加了 以删除图像底部的多余边距。\noindent\hspace{}\vspace

\documentclass{article}
\usepackage{graphicx, lipsum}
\begin{document}
\setlength{\fboxrule}{0pt}
\fboxsep=30pt
\newlength\tbmargin
\tbmargin=2pt
\newsavebox\mysavebox
\newenvironment{imgminipage}[2][]{\noindent\hspace{-\fboxsep}%
  \def\imgcmd{\includegraphics[width=\dimexpr\wd\mysavebox,%
   height=\dimexpr\ht\mysavebox+\dp\mysavebox-2\fboxsep+2\tbmargin\relax,#1]{#2}}%
\begin{lrbox}{\mysavebox}%
\begin{minipage}[c][][c]{1\textwidth}}{
\end{minipage}
\end{lrbox}%
\sbox\mysavebox{\fbox{\usebox\mysavebox}}%
\mbox{\rlap{\raisebox{\dimexpr\fboxsep-\dp\mysavebox-\tbmargin}{\imgcmd}}\usebox\mysavebox}\vspace{\dimexpr-\fboxsep+\tbmargin}%
}
\begin{imgminipage}{example-image}
blah blah blah
blah blah blah
blah blah blah
blah blah blah
blah blah blah
blah blah blah
blah blah blah
blah blah blah
blah blah blah
blah blah blah
blah blah blah
blah blah blah
blah blah blah
\end{imgminipage}

\lipsum[1]
\end{document}

在此处输入图片描述

相关内容