彩色框中的对象垂直居中

彩色框中的对象垂直居中

我想要创建一个像下图这样的结构。

在此处输入图片描述

这是一个彩色框,它应该与页面顶部保持一定距离。在这个框中,我想在左侧显示一张图片,在右侧打印任意数量的行。我要编译大量文档,因此我要自动执行此过程(使用 从一个 json 中读取文本内容luacode)。我使用的图片不会都具有相同的大小,而且文本中的点数在文档之间也有所不同。所以在另一种情况下,我希望它看起来像这样:

在此处输入图片描述

关键点是,右侧的图片和文本块都垂直居中在彩色框的中心。我尝试通过将 嵌入tabular另一个 来实现这一点tabular,但这样图片就无法居中。目前,我只是用

\filldraw[green] ($(current page.north west)+(0cm,-16cm)$) rectangle  ($(current page.south east)+(0cm,4cm)$);

我不知道如何将盒子位置链接到其中的对象。

我怎样才能实现这个目标?

答案1

我假设您愿意使用 tikz,因此我尝试按照您的意愿安排盒子及其内容(前提是我正确地得到了您需要的东西):

\documentclass[10pt,a4paper]{article}
\usepackage[utf8]{inputenc}
\usepackage[english]{babel}
\usepackage{amsmath}
\usepackage{amsfonts}
\usepackage{amssymb}
\usepackage{graphicx,tikz}
\usepackage{lmodern}
\usetikzlibrary{calc,positioning,fit}
\usepackage[left=0cm,right=0cm,top=2cm,bottom=2cm]{geometry}
\setlength{\parindent}{0pt}
\begin{document}
\begin{tikzpicture}
\coordinate (sw) at ($(current page.north west)+(0cm,-16cm)$);
\coordinate (ne) at ($(current page.south east)+(0cm,4cm)$);
\node[fit={(ne)(sw)},fill=green!10](colorbox){};
\node[draw,minimum size =4cm](pic)at({$(colorbox.north west)!1/3!(colorbox.north east)$}|-{$(colorbox.south west)!1/2!(colorbox.north west)$}){PIC};
\node[draw,text width=\linewidth/3](text)at({$(colorbox.north west)!2/3!(colorbox.north east)$}|-{$(colorbox.south east)!1/2!(colorbox.north east)$}){
\begin{itemize}
\item[•] cats have four legs
\item[•]
\item[•]
\item[•] there's much more to say about cats , so I need automatic linebreaks
\item[•]
\item[•] cats have four legs
\item[•]
\item[•]
\item[•] there's much more to say about cats , so I need automatic linebreaks
\end{itemize}};
\end{tikzpicture}
\end{document}

它生产:

在此处输入图片描述

答案2

另一种解决方案没有 TiKZ 机制,只有小包framed及其shaded环境:

\documentclass[10pt,a4paper]{article}
\usepackage[T1]{fontenc}
\usepackage[svgnames]{xcolor} %
\usepackage[export]{adjustbox}
\usepackage{lmodern}
\usepackage{framed, linegoal}

\colorlet{shadecolor}{LimeGreen}

\begin{document}

\begin{shaded}
\sffamily
\includegraphics[scale=0.4, valign=c]{kingsize_canary}
\quad\begin{minipage}[c]{\linegoal}
\begin{itemize}
\item cats have four legs
\item cats don’t like dogs (and conversely)
\item cats eat birds \& mice
\item there's much more to say about cats , so I need automatic linebreaks
\end{itemize}
\end{minipage}
\end{shaded}

\end{document} 

在此处输入图片描述

答案3

这将测量图片的尺寸并调整文本部分的宽度。根据您的喜好调整填充。

\documentclass{article}
\usepackage{xcolor,graphicx}

\newsavebox{\picturetextbox}
\newsavebox{\picturebox}
\newcommand{\picturebgcolor}{}% initialize

\newenvironment{picturetext}[4][]
 {% #1 options for the picture
  % #2 picture file name
  % #3 background color
  % #4 global width
  \renewcommand{\picturebgcolor}{#3}%
  \sbox{\picturebox}{\includegraphics[#1]{#2}}%
  \begin{lrbox}{\picturetextbox}
  \hspace{1em}%
  \begin{tabular}{@{}c@{}}\usebox{\picturebox}\end{tabular}%
  \hspace{1em}%
  \begin{minipage}{\dimexpr#4-5em-\wd\picturebox\relax}\raggedright
 }
 {%
  \end{minipage}
  \end{lrbox}%
  \setlength{\fboxsep}{1em}%
  \colorbox{\picturebgcolor}{\usebox{\picturetextbox}}%
 }

\begin{document}

\begin{center}
\begin{picturetext}[width=5cm]{example-image}{green}{\textwidth}
  \begin{itemize}
  \item cats eat meat
  \item there's much more to say about cats, so I need automatic linebreaks
  \item there's much more to say about cats, so I need automatic linebreaks
  \item there's much more to say about cats, so I need automatic linebreaks
  \end{itemize}
\end{picturetext}
\end{center}

\begin{center}
\begin{picturetext}[width=2cm]{example-image-9x16}{green}{0.75\textwidth}
  \begin{itemize}
  \item cats eat meat
  \item there's much more to say about cats, so I need automatic linebreaks
  \end{itemize}
\end{picturetext}
\end{center}

\end{document}

在此处输入图片描述

相关内容