我想要在 5 厘米宽的照片旁边显示两个文本框。文本框的总高度可能大于或小于照片的高度。
如果组合高度小于照片,我希望文本框与照片的顶部和底部垂直对齐,因此:
+---------------------------------------+ +------------+
| Text1 text1 text1 text1 text1 text1 | | |
| text1 text1 text1 text1 text1. | | |
+---------------------------------------+ | |
| Photo |
| |
+---------------------------------------+ | |
| Text2 text2 text2 text2 text2 text2 | | |
| text2 text2 text2 text2 text2. | | |
+---------------------------------------+ +------------+
如果总高度大于照片,我希望两个框彼此位于下方,因此:
+---------------------------------------+ +------------+
| Text1 text1 text1 text1 text1 text1 | | |
| text1 text1 text1 text1 text1 text1 | | |
| text1 text1 text1 text1 text1 text1 | | |
| text1 text1 text1 text1 text1 text1 | | Photo |
+---------------------------------------+ | |
+---------------------------------------+ | |
| Text2 text2 text2 text2 text2 text2 | | |
| text2 text2 text2 text2 text2 text2 | | |
| text2 text2 text2 text2 text2 text2 | +------------+
| text2 text2 text2 text2 text2. |
+---------------------------------------+
有什么方法可以自动实现这一点吗?如果没有,我怎样才能最好地实现第一个插图中所示的效果?(我已经尝试了表格/多行设置和浮点数,但就是做不到。)
编辑:
这是我尝试过但没有成功的事情之一(用合适的文件名替换 1988.jpg):
\documentclass{report}
\usepackage{graphicx}
\usepackage{multirow}
\begin{document}
\begin{tabular}{l r}
\fbox{\parbox[t]{8cm}{Text1 text1 text1 text1 text1 text1 text1 text1 text1 text1
text1 text1 text1 text1 text1.}}
& \multirow{3}{*}{\parbox[t]{5cm}{\includegraphics[width=5cm,height=10cm]{1988.jpg}
}} \\
\parbox{8cm}{\vfill} & \\
\fbox{\parbox[b]{8cm}{Text2 text2 text2 text2 text2 text2 text2 text2 text2 text2
text2 text2 text2 text2 text2.}} & \\
\end{tabular}
\end{document}
答案1
您可以将文本和图像放在框中,测量它们的高度,然后采取相应的措施:
\documentclass{article}
\usepackage{graphicx}
\usepackage{adjustbox}
\newlength\PhotoWd
\setlength\PhotoWd{5cm}
\newlength\PhotoGap
\setlength\PhotoGap{20pt}
\newlength\TextWd
\setlength\TextWd{\dimexpr\textwidth-\PhotoWd-\PhotoGap\relax}
\newlength\TextHt
\newlength\ImageHt
\newlength\MaxHt
\newsavebox\TextBox
\newsavebox\ImageBox
\newcommand\PlaceTextImage[4][]{%
\begin{lrbox}{\TextBox}
\begin{minipage}{\linewidth}
\fbox{\parbox{\dimexpr\TextWd-2\fboxsep-2\fboxrule\relax}{#2}}\par\medskip
\fbox{\parbox{\dimexpr\TextWd-2\fboxsep-2\fboxrule\relax}{#3}}
\end{minipage}
\end{lrbox}%
\savebox\ImageBox{\includegraphics[#1]{#4}}%
\setlength\TextHt{\dimexpr\ht\TextBox+\dp\TextBox\relax}%
\setlength\ImageHt{\dimexpr\ht\ImageBox+\dp\ImageBox\relax}%
\setlength\MaxHt{\TextHt}
\ifdim\ImageHt>\TextHt\relax
\setlength\MaxHt{\ImageHt}
\fi%
\par\medskip\noindent
\begin{minipage}[t][\MaxHt][t]{\TextWd}
\fbox{\parbox[t]{\dimexpr\TextWd-2\fboxsep-2\fboxrule\relax}{#2}}%
\ifdim\ImageHt>\TextHt\relax
\par\vfill
\else
\par\medskip
\fi%
\fbox{\parbox[t]{\dimexpr\TextWd-2\fboxsep-2\fboxrule\relax}{#3}}
\end{minipage}\hspace*{\PhotoGap}%
\begin{minipage}[t][\MaxHt][t]{\PhotoWd}
\adjustbox{valign=t}{\usebox\ImageBox}
\end{minipage}\par\medskip%
}
\begin{document}
Text before
\PlaceTextImage[width=\PhotoWd,height=200pt]{some test text test text test text test text test text test text test text test text test text test text test text test text test text test text test text}{some other text test text test text test text test text test text test text test text test text test text test text test text test text test text test text test text test text test text test text test text test text test text test text test text test text}{example-image-b}
Text after and before
\PlaceTextImage[width=\PhotoWd,height=40pt]{some test text test text test text test text test text test text test text test text test text test text test text test text test text test text test text}{some other text test text test text test text test text test text test text test text test text test text test text test text test text test text test text test text test text test text test text test text test text test text test text test text test text}{example-image-b}
Text after
\end{document}
\PlaceTextImage
有三个强制参数:
\PlaceTextImage{<upper text>}{<lower text>}{<image file>}
以及一个可选参数,允许您指定选项\includegraphics
。
使用长度\PhotoWd
,\PhotoGap
您可以更改照片的宽度以及文本和照片之间的间隙;文本框的宽度是自动计算的。
答案2
可以使用多个小页面来适当地换行和对齐文本来实现这一点。
\documentclass{report}
\usepackage[demo]{graphicx} % Remove [demo] for final version
\usepackage[export]{adjustbox}
\usepackage{calc,etoolbox}
\usepackage{fullpage} % Increase margins for these specific widths
\begin{document}
\newcommand{\imagebox}{\includegraphics[width=5cm,height=10cm,valign=t]{1988.jpg}}
\newcommand{\textboxes}{
\noindent\begin{minipage}[t]{\linewidth}
\fbox{\parbox[t]{8cm}{
Text1 text1 text1 text1 text1 text1 text1 text1 text1
text1 text1 text1 text1 text1 text1 text1 text1 text1
text1 text1 text1 text1 text1 text1 text1 text1 text1
text1 text1 text1 text1 text1 text1 text1 text1 text1
text1 text1 text1 text1 text1 text1 text1 text1 text1
text1 text1 text1 text1 text1 text1 text1 text1 text1
text1 text1 text1 text1 text1 text1 text1 text1 text1
text1 text1 text1 text1 text1 text1 text1 text1 text1
text1 text1 text1 text1 text1.}}
\par\vspace{1ex}\vfill
\fbox{\parbox[b]{8cm}{
Text2 text2 text2 text2 text2 text2 text2 text2 text2
text2 text2 text2 text2 text2 text2 text2 text2 text2
text2 text2 text2 text2 text2 text2 text2 text2 text2
% text2 text2 text2 text2 text2 text2 text2 text2 text2
% text2 text2 text2 text2 text2 text2 text2 text2 text2
% text2 text2 text2 text2 text2 text2 text2 text2 text2
% text2 text2 text2 text2 text2 text2 text2 text2 text2
% text2 text2 text2 text2 text2 text2 text2 text2 text2
% text2 text2 text2 text2 text2 text2 text2 text2 text2
% text2 text2 text2 text2 text2 text2 text2 text2 text2
% text2 text2 text2 text2 text2 text2 text2 text2 text2
% text2 text2 text2 text2 text2 text2 text2 text2 text2
% text2 text2 text2 text2 text2 text2 text2 text2 text2
% text2 text2 text2 text2 text2 text2 text2 text2 text2
% text2 text2 text2 text2 text2 text2 text2 text2 text2
% text2 text2 text2 text2 text2 text2 text2 text2 text2
% text2 text2 text2 text2 text2 text2 text2 text2 text2
text2 text2 text2 text2 text2 text2 text2 text2 text2
text2 text2 text2 text2 text2.}}
\end{minipage}
}
% Calculate height of image and text boxes
\newlength{\imageboxheight}
\settototalheight{\imageboxheight}{\imagebox}
\newlength{\textboxesheight}
\settototalheight{\textboxesheight}{\textboxes}
\newlength{\maxTextBoxes}
\setlength{\maxTextBoxes}{\ifdimgreater{\textboxesheight}{\imageboxheight}{\textboxesheight}{\imageboxheight}}
% Typeset content
\noindent\begin{minipage}[t][\maxTextBoxes]{\textwidth}
\noindent\begin{minipage}[t][\imageboxheight]{9cm}
\textboxes\end{minipage}\begin{minipage}[t]{6cm}
\imagebox
\end{minipage}
\end{minipage}\vspace{\baselineskip} % A bit of extra space
% Following text starts after both image and text
Next paragraph starts here.
\end{document}
答案3
这是此类构造的界面。
\documentclass{article}
\usepackage{graphicx}
\usepackage[export]{adjustbox}
\usepackage{environ}
\usepackage{calc}
\newsavebox{\photobox}
\newsavebox{\upperbox}
\newsavebox{\lowerbox}
\newsavebox{\tempbox}
\newlength{\photodepth}
\newlength{\boxesdepth}
\newlength{\photowidth}
\newenvironment{boxesandphoto}[2][]
{\vfuzz=2pt
\par\addvspace{\topsep}
\sbox{\photobox}{\includegraphics[#1,valign=t]{#2}}%
\setlength{\photowidth}{\wd\photobox}%
\setlength{\photodepth}{\dp\photobox}%
}
{%
\maketextboxes\centering
\ifdim\photodepth<\boxesdepth \photodepth=\boxesdepth \fi
\addtolength\photodepth{\ht\photobox}%
\addtolength\photodepth{\fboxrule+\fboxsep}
\leavevmode
\vtop to\photodepth{
\vspace*{0pt}
\unvbox\tempbox
}\hfill
\vtop to\photodepth{
\vspace*{0pt}
\box\photobox
\vss
}
\par\addvspace{\dimexpr\topsep+\medskipamount}
}
\newcommand{\maketextbox}[2]{%
\sbox{\tempbox}{%
\fbox{%
\begin{minipage}[#1]{\textwidth-\photowidth-2\fboxsep-2\fboxrule-1em}
\BODY\par
\vspace{0pt}
\end{minipage}%
}%
}
\global\setbox#2=\box\tempbox
}
\NewEnviron{toptext}{\maketextbox{t}{\upperbox}}
\NewEnviron{bottomtext}{\maketextbox{b}{\lowerbox}}
\newcommand{\maketextboxes}{%
\setbox\tempbox=\vtop{
\box\upperbox
\vspace{2ex plus 1fill}
\box\lowerbox
\kern\fboxrule\kern\fboxsep
}%
\setlength{\boxesdepth}{\dp\tempbox}%
\addtolength{\boxesdepth}{-\fboxsep-\fboxrule}
}
\begin{document}
something above
\begin{boxesandphoto}[height=3cm,width=3cm]{example-image-b}
\begin{toptext}
some test text test text test text test text test text test text
test text test text test text test text test text test text test
text test text test text
\end{toptext}
\begin{bottomtext}
some other text test text test text test text test text test text
test text test text test text test text test text test text test text
test text test text test text test text test text test text test text
test text test text test text test text test text
\end{bottomtext}
\end{boxesandphoto}
something in the middle
\begin{boxesandphoto}[height=5cm,width=3cm]{example-image-b}
\begin{toptext}
some test text test text test text test text test text test text
test text test text test text test text test text test text test
text test text test text
\end{toptext}
\begin{bottomtext}
some other text test text test text test text test text test text
test text test text test text test text test text test text test text
test text test text test text test text test text test text test text
test text test text test text test text test text
\end{bottomtext}
\end{boxesandphoto}
something at the bottom
\end{document}
答案4
这是对 cslstr 解决方案的轻微修改。在这里,我避免在命令中使用 minipage 环境\textboxes
。这对我来说很有效。
\documentclass{report}
\usepackage[demo]{graphicx} % Remove [demo] for final version
\usepackage[export]{adjustbox}
\usepackage{calc,etoolbox}
\usepackage{fullpage} % Increase margins for these specific widths
\begin{document}
\newcommand{\imagebox}{\includegraphics[width=5cm,height=10cm,valign=t]{1988.jpg}}
\newcommand{\textboxes}{
\fbox{\parbox[t]{8cm}{
Text1 text1 text1 text1 text1 text1 text1 text1 text1
text1 text1 text1 text1 text1 text1 text1 text1 text1
text1 text1 text1 text1 text1 text1 text1 text1 text1
text1 text1 text1 text1 text1 text1 text1 text1 text1
text1 text1 text1 text1 text1 text1 text1 text1 text1
text1 text1 text1 text1 text1 text1 text1 text1 text1
text1 text1 text1 text1 text1 text1 text1 text1 text1
text1 text1 text1 text1 text1 text1 text1 text1 text1
text1 text1 text1 text1 text1.}}
\par\vspace{1ex}\vfill
\fbox{\parbox[b]{8cm}{
Text2 text2 text2 text2 text2 text2 text2 text2 text2
text2 text2 text2 text2 text2 text2 text2 text2 text2
text2 text2 text2 text2 text2 text2 text2 text2 text2
% text2 text2 text2 text2 text2 text2 text2 text2 text2
% text2 text2 text2 text2 text2 text2 text2 text2 text2
% text2 text2 text2 text2 text2 text2 text2 text2 text2
% text2 text2 text2 text2 text2 text2 text2 text2 text2
% text2 text2 text2 text2 text2 text2 text2 text2 text2
% text2 text2 text2 text2 text2 text2 text2 text2 text2
% text2 text2 text2 text2 text2 text2 text2 text2 text2
% text2 text2 text2 text2 text2 text2 text2 text2 text2
% text2 text2 text2 text2 text2 text2 text2 text2 text2
% text2 text2 text2 text2 text2 text2 text2 text2 text2
% text2 text2 text2 text2 text2 text2 text2 text2 text2
% text2 text2 text2 text2 text2 text2 text2 text2 text2
% text2 text2 text2 text2 text2 text2 text2 text2 text2
% text2 text2 text2 text2 text2 text2 text2 text2 text2
text2 text2 text2 text2 text2 text2 text2 text2 text2
text2 text2 text2 text2 text2.}}
}
\newcommand{\textboxespaged}{
\noindent\begin{minipage}[t]{\linewidth}
\textboxes%
\end{minipage}
}
% Calculate height of image and text boxes
\newlength{\imageboxheight}
\settototalheight{\imageboxheight}{\imagebox}
\newlength{\textboxesheight}
\settototalheight{\textboxesheight}{\textboxespaged}
\newlength{\maxTextBoxes}
\setlength{\maxTextBoxes}{\ifdimgreater{\textboxesheight}{\imageboxheight}{\textboxesheight}{\imageboxheight}}
% Typeset content
\noindent\begin{minipage}[t][\maxTextBoxes]{\textwidth}
\noindent\begin{minipage}[t][\imageboxheight]{9cm}
\textboxes\end{minipage}\begin{minipage}[t]{6cm}
\imagebox
\end{minipage}
\end{minipage}\vspace{\baselineskip} % A bit of extra space
% Following text starts after both image and text
Next paragraph starts here.
\end{document}