如何优雅地在图形旁边添加标题(带控制参数)?

如何优雅地在图形旁边添加标题(带控制参数)?

我需要一个图形函数来将标题放在旁边。

图形比例应控制在“xtextwidth” 和标题比例为“ytextwidth”。到目前为止一切都很好!

该函数应控制图形与参数“1”和“2”左侧和右侧的红色垂直边框的距离。

同样,参数“3”和“4”控制字幕与绿色垂直边框的距离。

在此处输入图片描述

我已经尽力了,但还是没能实现。如果有人能帮我创建这个模板/序言,我将不胜感激。

这是我目前所拥有的:

\begin{figure}[!h]
\floatbox[{\capbeside\thisfloatsetup{capbesideposition={right,center},capbesidewidth=4cm,floatrowsep=quad}}]{figure}[\FBwidth]
{\caption{xyzxyz xyz xyz xyz xyz.}\label{Fig:1}}
{\centering\includegraphics[width=0.5\textwidth]{chapter-1/Figures/1.png}} 
\end{figure}

它创造了类似的东西!但它过多地基于反复试验。

在此处输入图片描述

编辑/更新:

如果我按照@leandriis 提供的答案,它会产生类似这样的结果:

我希望能够控制图形的左边距和标题的右边距。我该怎么做?

在此处输入图片描述

解决方案:

Werner 的解决方案很有效!如果您使用 \usepackage{multicol},请查看我的评论。

答案1

以下是您需要的键值方法。您可以设置的键包括

  • left- 左边距/间隙(默认 = 0pt
  • right- 右边距/间隙 (默认 = 0pt)
  • figcapsep- 图形/图像与标题之间的水平间隙(默认 = \tabcolsep
  • figwidth- 图形/图像的宽度(如果未指定则计算)
  • capwidth- 标题的宽度(若未指定则计算)
  • figlabel-\label应该与\caption
  • captiontoc- 中使用的可选参数\caption[<ToC>]{<text>}

整个结构适合\linewidth

在此处输入图片描述

\documentclass{article}

\usepackage[margin=1in,showframe]{geometry}% Just for this example
\usepackage[nopar]{lipsum}% Just for this example
\usepackage{xkeyval,float}
\usepackage[export]{adjustbox}

\makeatletter
\define@cmdkey{iac}{left}[0pt]{}% \cmdKV@iac@left
\define@cmdkey{iac}{right}[0pt]{}% \cmdKV@iac@right
\define@cmdkey{iac}{figcapsep}[\tabcolsep]{}% \cmdKV@iac@figcapsep
\define@cmdkey{iac}{figwidth}[\relax]{}% \cmdKV@iac@figwidth
\define@cmdkey{iac}{capwidth}[\relax]{}% \cmdKV@iac@capwidth
\define@cmdkey{iac}{figlabel}[\relax]{}% \cmdKV@iac@figlabel
\define@cmdkey{iac}{captiontoc}[\relax]{}% \cmdKV@iac@captiontoc

\newcommand{\imageandcaption}[3][]{%
  \setkeys{iac}{
    left,right,figcapsep,figwidth,capwidth,figlabel,captiontoc,% Set default keys
    #1% Set updated keys
  }%
  \expandafter\ifx\cmdKV@iac@figwidth\relax% No figure width supplied
    \expandafter\ifx\cmdKV@iac@capwidth\relax% No caption width supplied
      \def\cmdKV@iac@figwidth{.5\dimexpr\linewidth-\cmdKV@iac@left-\cmdKV@iac@right-\cmdKV@iac@figcapsep}%
      \def\cmdKV@iac@capwidth{\cmdKV@iac@figwidth}%
    \else% Caption width provided
      \def\cmdKV@iac@figwidth{\dimexpr\linewidth-\cmdKV@iac@left-\cmdKV@iac@right-\cmdKV@iac@figcapsep-\cmdKV@iac@capwidth}%
    \fi
  \else% Figure width supplied
    \expandafter\ifx\cmdKV@iac@capwidth\relax% No caption width supplied
      \def\cmdKV@iac@capwidth{\dimexpr\linewidth-\cmdKV@iac@left-\cmdKV@iac@right-\cmdKV@iac@figcapsep-\cmdKV@iac@figwidth}%
    \fi
  \fi
  \hspace*{\cmdKV@iac@left}% Indent from left margin
  \includegraphics[valign=c,width=\cmdKV@iac@figwidth]{#2}% Insert image
  \hspace*{\cmdKV@iac@figcapsep}% Space between image and caption
  \begin{minipage}{\cmdKV@iac@capwidth}
    \expandafter\ifx\cmdKV@iac@captiontoc\relax
      \caption{#3}% \caption without ToC entry
    \else
      \caption[\cmdKV@iac@captiontoc]{#3}% \caption with ToC entry
    \fi
    \expandafter\ifx\cmdKV@iac@figlabel\relax\else
      \label{\cmdKV@iac@figlabel}%
    \fi
  \end{minipage}%
  \hspace*{\cmdKV@iac@right}% Space on right of construction
}
\makeatother

\begin{document}

See Figure~\ref{fig:figure1}.\sloppy

\begin{figure}[H]
  \imageandcaption[
    left=10pt,
    figwidth=.3\linewidth,
    capwidth=.6\linewidth,
    figlabel=fig:figure1%
  ]
  {example-image-a}{This is a caption}
\end{figure}

\lipsum[2]

\begin{figure}[H]
  \raggedleft
  \imageandcaption[
    right=10pt,
    figwidth=.6\linewidth,
    capwidth=.3\linewidth,
    figlabel=fig:figure2]
  {example-image-b}{\lipsum[2]}
\end{figure}

\lipsum[2]

\begin{figure}[H]
  \imageandcaption
    {example-image-c}{\lipsum[2]}
\end{figure}

\end{document}

答案2

使用以下自定义命令,\setmyimagewidth您可以设置当前图像的宽度。如果您随后将用作命令width=\myimagewidth的选项并将 用作 的选项,则标题的图像宽度将适应整体文本宽度,自动将图像的左边框与左边距对齐,将标题的右边框与右边距对齐:\includegraphicscapbesidewidth=\mycaptionwidth\thisfloatsetup

在此处输入图片描述

\documentclass{article}
\usepackage{floatrow}
\usepackage{graphicx}
\usepackage{calc}
\usepackage{lipsum}

\newlength{\myimagewidth}
\newlength{\mycaptionwidth}
\setlength{\mycaptionwidth}{\textwidth-\myimagewidth-10pt}

\newcommand{\setmyimagewidth}[1]{\setlength{\myimagewidth}{#1}
\setlength{\mycaptionwidth}{\textwidth-\myimagewidth-10pt}}
\begin{document}
%\lipsum[1]
\begin{figure}[!h]
\setmyimagewidth{0.55\textwidth}
\floatbox[{\capbeside\thisfloatsetup{capbesideposition={right,center},capbesidewidth=\mycaptionwidth,floatrowsep=quad}}]{figure}[\FBwidth]
{\caption{xyzxyz xyz xyz xyz xyz. xyzxyz xyz xyz xyz xyz. xyzxyz xyz xyz xyz xyz. xyzxyz xyz xyz xyz xyz.xyzxyz xyz xyz xyz xyz.xyzxyz xyz xyz xyz xyz.}\label{Fig:1}}
{\centering\includegraphics[width=\myimagewidth]{example-image}} 
\end{figure}
\lipsum[4]
\begin{figure}[!h]
\setmyimagewidth{0.15\textwidth}
\floatbox[{\capbeside\thisfloatsetup{capbesideposition={right,center},capbesidewidth=\mycaptionwidth,floatrowsep=quad}}]{figure}[\FBwidth]
{\caption{xyzxyz xyz xyz xyz xyz. xyzxyz xyz xyz xyz xyz. xyzxyz xyz xyz xyz xyz. xyzxyz xyz xyz xyz xyz.xyzxyz xyz xyz xyz xyz.xyzxyz xyz xyz xyz xyz.}\label{Fig:1}}
{\centering\includegraphics[width=\myimagewidth]{example-image}} 
\end{figure}
\lipsum[4]
\begin{figure}[!h]
\setmyimagewidth{0.3\textwidth}
\floatbox[{\capbeside\thisfloatsetup{capbesideposition={right,center},capbesidewidth=\mycaptionwidth,floatrowsep=quad}}]{figure}[\FBwidth]
{\caption{xyzxyz xyz xyz xyz xyz. xyzxyz xyz xyz xyz xyz. xyzxyz xyz xyz xyz xyz. xyzxyz xyz xyz xyz xyz.xyzxyz xyz xyz xyz xyz.xyzxyz xyz xyz xyz xyz.}\label{Fig:1}}
{\centering\includegraphics[width=\myimagewidth]{example-image}} 
\end{figure}
\end{document}

相关内容