我正在尝试创建一个包含文本和图像的圆角框。 期望的结果如下:
图像的左边缘是圆的。
我已经管理了除图像部分之外的所有内容,mdframed
如下所示:
\documentclass[a4paper,11pt]{article}
\usepackage[utf8]{inputenc}
\usepackage{graphicx}
\usepackage{color}
\usepackage[margin=0pt,bottom=1em]{geometry}
\usepackage{tikz}
\usetikzlibrary[shadows]
\usepackage[framemethod=TikZ]{mdframed}
\mdfdefinestyle{item}{roundcorner=10pt,
leftmargin=20pt,
rightmargin=20pt,
backgroundcolor=gray,
innertopmargin=.5em,
innerbottommargin=10pt,
innerleftmargin=200pt,
middlelinewidth=0pt,
font=\normalfont\large,
shadow=true,
frametitlefont=\normalfont\Large\bfseries,
frametitleaboveskip=1em}
\begin{document}
\vfill % autospace boxes when there are more than one
\begin{mdframed}[style=item,frametitle=title]
Auto wrapped text aaa aaaa a aaa a aaaaa aaaaaaa aaaa a aaa a aaaaa aaaaaaa aaaa a aaa a aaaaa aaaaaaa aaaa a aaa a aaaaa aaaaaaa aaaa a aaa a aaaaa aaaaaaa aaaa a aaa a aaaaa aaaaaaa aaaa a aaa a aaaaa aaaaaaa aaaa a aaa a aaaaa aaaaaaa aaaa a aaa a aaaaa aaaaaaa aaaa a aaa a aaaaa aaaaaaa aaaa a aaa a aaaaa aaaaaaa aaaa a aaa a aaaaa aaaaaaa aaaa a aaa a aaaaa aaaa
\end{mdframed}
\end{document}
如果图像缩放以适合框的高度,然后裁剪文本,那就很好了,但只要保留纵横比,我就可以调整我的图像以适应大多数方式。
有什么想法如何从这里继续下去吗?有没有比使用更好的方法mdframed
?
答案1
无需使用额外的框,您可以简单地使用singleextra
(对于不间断框架)和 TikZ\node
和\draw
命令来绘制垂直规则并将图像放置在所需的位置;一个完整的示例(当然,根据您的需要调整使用的长度):
\documentclass[a4paper,11pt]{article}
\usepackage[utf8]{inputenc}
\usepackage[margin=0pt,bottom=1em]{geometry}
\usepackage{graphicx}
\usepackage{tikz}
\usetikzlibrary[shadows]
\usepackage[framemethod=TikZ]{mdframed}
\usetikzlibrary{calc}
\usepackage{lipsum}% just to generate text for the example
\mdfdefinestyle{item}{roundcorner=10pt,
leftmargin=20pt,
rightmargin=20pt,
backgroundcolor=gray,
innertopmargin=.5em,
innerbottommargin=10pt,
innerleftmargin=200pt,
middlelinewidth=0pt,
font=\normalfont\large,
shadow=true,
frametitlefont=\normalfont\Large\bfseries,
frametitleaboveskip=1em,
singleextra={
\draw[black,ultra thick]
([xshift=190pt]P-|O) -- ([xshift=190pt]O);
\path let \p1 = ( $ (O)!0.5!(P) $ ) in
(\the\dimexpr0.5\mdflength{innerleftmargin}\relax,\y1) coordinate (Q);
\node at (Q) {\includegraphics[width=\the\dimexpr0.7\mdflength{innerleftmargin}\relax]{verde}};}
}
\begin{document}
\begin{mdframed}[style=item,frametitle=title]
\lipsum[4]
\end{mdframed}
\vspace*{1cm}
\begin{mdframed}[style=item,frametitle=title]
\lipsum[1-2]
\end{mdframed}
\end{document}
生成的文档的图像:
如果帧必须分割,并且图像必须放置在一个特定的位置,那么也可以使用firstextra
、等。secondextra
答案2
在我看来,将您的图片与您的主张进行定位可能很困难。
我建议以不同的方式进行:
1/ 在保存的框中排版文本,以计算文本的高度
\newsavebox{\tmpbox}
\savebox{\tmpbox}{\parbox{...width...}{...content...}}
2/ 假设您知道图片的最大宽度,您将能够包含具有正确纵横比的图片。我使用\parbox
将其居中到基线。请注意,您需要calc
包
\newlength\mymeasure
\settototalheight{\mymeasure}{\usebox{\tmpbox}}
\parbox{...maxwidth...}{%
\centering%
\includegraphics[width=...maxwidth...,height=\mymeasure,keepaspectratio]{...picture path...}%
}
\usebox{\tmpbox} % use the text box
3/ 您可以将上面的线条放在mdframed
绘制框架中
例如:
\documentclass[a4paper,11pt]{article}
\usepackage[utf8]{inputenc}
\usepackage{graphicx}
\usepackage{tikz}
\usepackage{calc}
\usetikzlibrary[shadows]
\usepackage[framemethod=TikZ]{mdframed}
\mdfdefinestyle{item}{roundcorner=10pt,
leftmargin=20pt,
rightmargin=20pt,
backgroundcolor=gray,
innermargin=10pt,
shadow=true}
\begin{document}
\begin{mdframed}[style=item]
\newsavebox{\tmpbox}
\savebox{\tmpbox}{\parbox{.5\columnwidth}{%
\textbf{\Large Title}
Auto wrapped text aaa aaaa a aaa a aaaaa aaaaaaa aaaa a aaa a aaaaa aaaaaaa aaaa a aaa a aaaaa aaaaaaa aaaa a aaa a aaaaa aaaaaaa aaaa a aaa a aaaaa aaaaaaa aaaa a aaa a aaaaa aaaaaaa aaaa a aaa a aaaaa aaaaaaa aaaa a aaa a aaaaa aaaaaaa aaaa a aaa a aaaaa aaaaaaa aaaa a aaa a aaaaa aaaaaaa aaaa a aaa a aaaaa aaaaaaa aaaa a aaa a aaaaa aaaaaaa aaaa a aaa a aaaaa aaaa
}}
\newlength\mymeasure
\settototalheight{\mymeasure}{\usebox{\tmpbox}}
\parbox{.4\columnwidth}{%
\centering%
\includegraphics[width=.3\columnwidth,height=\mymeasure,keepaspectratio]{test}%
}
\usebox{\tmpbox}
\end{mdframed}
\end{document}
答案3
感谢 Lionel 和 Gonzalo 的回答以及来自的图像缩放根据给定的宽度和高度,将包含的图形缩放到更高的比例,而不是更低的比例并且\clip
通过圆角命令我设法得到了我想要的东西。
它首先像@LionelMANSUY 那样在框中创建文本,然后重新缩放和舍入图像并通过singleextra
@GonzaloMedina 答案将其插入
\documentclass[a4paper,11pt]{article}
\usepackage[utf8]{inputenc}
\usepackage{graphicx}
\graphicspath{ {./meta/} }
\usepackage[margin=0pt,bottom=1em]{geometry}
\usepackage{tikz}
\usepackage{calc}
\usepackage[export]{adjustbox}
\usepackage{lipsum}
\usetikzlibrary[shadows]
\usetikzlibrary{calc}
\usepackage[framemethod=TikZ]{mdframed}
\newlength\ztextheight
\newlength\zimwidth\setlength{\zimwidth}{130pt} % Image width
\newlength\zimmargin\setlength{\zimmargin}{20pt} % Image<>Text margin
\newlength\ztextwidth\setlength{\ztextwidth}{530pt} % Text width
\newlength\zrradius\setlength{\zrradius}{10pt} % Rounding radius
\newlength\zitemmargin
\setlength{\zitemmargin}{\zimwidth+\zimmargin}
\newsavebox{\zitembox}
% Usage: \zitem{title}{body}{image.jpg}
\newcommand{\zitem}[3]{
\savebox{\zitembox}{\parbox{\ztextwidth-\zitemmargin}{%
\vspace{10pt} % top inner margin
\textbf{\Large #1}
\vspace{5pt}\newline % title/text margin
\large #2
\vspace{10pt} % bottom inner margin
}}
\settototalheight{\ztextheight}{\usebox{\zitembox}}
\begin{mdframed}[style=item,
singleextra={
% Crop image, rounding the left corners
\clip (\zimwidth,0pt) -- (\zimwidth, \ztextheight)
{[rounded corners=\zrradius] -- (0pt,\ztextheight) --
(0,0pt) -- (\zimwidth,0)};
% Scale image to fill both width and height
\node at (.5\zimwidth,.5\ztextheight) {
\adjustbox{min size={\zimwidth}{\ztextheight}}{
\includegraphics[max size={\zimwidth}{\ztextheight}]{#3}
}};
}]
\usebox{\zitembox}
\end{mdframed}
}
% Those marked with % are required to stay fixed
\mdfdefinestyle{item}{roundcorner=\zrradius,
leftmargin=20pt,
rightmargin=20pt,
backgroundcolor=white,
innertopmargin=0,%
innerbottommargin=0pt,%
innerleftmargin=\zitemmargin,%
middlelinewidth=0pt,
font=\normalfont\large,
shadow=true,
skipbelow=10pt,
skipabove=10pt
}
\begin{document}
\zitem{Title}{\lipsum[1] text}{lena.jpg}
\zitem{Title 2}{\lipsum[4]}{sailboat.jpg}
\zitem{Title 3}{\lipsum[2-3]}{clock.jpg}
\end{document}