如何让图形框架向内扩展?

如何让图形框架向内扩展?

我怎样才能让红色框架向内扩展?例如,下面的代码生成一个向外扩展的框架。我怎样才能让它向内扩展,这样它就不会占用图形之外的任何额外空间?例如\frame(color=(1,0,0),width=0.1mm,expand_inwards=True){\includegraphics[width=\imagewidth]{example-image-a}}

\documentclass{article}

\usepackage{graphicx}

\begin{document}


\begin{figure}
    \centering
    \def\imagewidth{0.12\textwidth}%
    \includegraphics[width=\imagewidth]{example-image-a}\\
    \frame{\includegraphics[width=\imagewidth]{example-image-a}}
    \caption{Caption}
    \label{fig:my_label}
\end{figure}


\end{document}

在此处输入图片描述

答案1

\fbox和之间存在有趣的交互\includegraphics,因此我的解决方案是排版具有零宽度和\fbox适当尺寸的图形。

\documentclass{article}
\usepackage{graphicx}

\NewDocumentCommand{\frameincludegraphics}{O{}m}{%
  \begingroup
  \sbox{0}{\includegraphics[#1]{#2}}%
  \setlength{\fboxrule}{0.4mm}% exaggerated to better see the effect
  \setlength{\fboxsep}{-\fboxrule}%
  \makebox[0pt][l]{\usebox{0}}%
  \fbox{\rule{0pt}{\ht0}\rule{\wd0}{0pt}}%
  \endgroup
}

\begin{document}

\begin{figure}
\centering

\includegraphics[width=0.12\textwidth]{example-image-a}

\frameincludegraphics[width=0.12\textwidth]{example-image-a}

\caption{Caption}
\label{fig:my_label}

\end{figure}

\end{document}

我这样做0.4mm只是为了让框架更加清晰可见。

在此处输入图片描述

如果您想要彩色框架,可以添加尾随颜色可选参数。

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

\NewDocumentCommand{\frameincludegraphics}{O{}mO{black}}{%
  \begingroup
  \sbox{0}{\includegraphics[#1]{#2}}%
  \setlength{\fboxrule}{0.4mm}%
  \setlength{\fboxsep}{-\fboxrule}%
  \makebox[0pt][l]{\usebox{0}}%
  {\color{#3}\fbox{\rule{0pt}{\ht0}\rule{\wd0}{0pt}}}%
  \endgroup
}

\begin{document}

\begin{figure}
\centering

\includegraphics[width=0.12\textwidth]{example-image-a}

\frameincludegraphics[width=0.12\textwidth]{example-image-a}

\frameincludegraphics[width=0.12\textwidth]{example-image-a}[red!60]

\caption{Caption}
\label{fig:my_label}

\end{figure}

\end{document}

在此处输入图片描述

答案2

如果您使用\frame\fbox\framebox,则问题在于,某些规则会在内容之前绘制。因此,您必须修剪内容以避免其超出已绘制的规则。另一个建议是,先绘制图像,然后绘制具有所需大小的框架:

\documentclass{article}

\usepackage{graphicx}

\newlength{\imageheight}

\begin{document}
%\setlength{\fboxrule}{1ex}% Try to see what happens with fat rules

\begin{figure}
    \centering
    \def\imagewidth{0.12\textwidth}%
    \settoheight{\imageheight}{\includegraphics[width=\imagewidth]{example-image-a}}%
    \fboxsep=0pt
    \includegraphics[width=\imagewidth]{example-image-a}\\
    \makebox[0pt][l]{\includegraphics[width=\imagewidth]{example-image-a}}%
    \raisebox{\fboxrule}{\framebox[\imagewidth]{\rule{0pt}{\dimexpr\imageheight-2\fboxrule}}}%
    \caption{Caption}
    \label{fig:my_label}
\end{figure}

\end{document}

在此处输入图片描述

或者,您可以使用包按规则宽度剪辑内容trimclip

\documentclass{article}

\usepackage{graphicx,trimclip}

\begin{document}

\begin{figure}
    \centering
    \def\imagewidth{0.12\textwidth}%
    \fboxsep=0pt
    \includegraphics[width=\imagewidth]{example-image-a}\\
    \framebox[\imagewidth]{\clipbox{\fboxrule}{\includegraphics[width=
        \imagewidth]{example-image-a}}}\\
    \setlength{\fboxrule}{1ex}
    \fbox{\clipbox{\fboxrule}{\includegraphics[width=
        \imagewidth]{example-image-a}}}\\
    \caption{Caption}
    \label{fig:my_label}
\end{figure}

\end{document}

在此处输入图片描述

您还可以\color在之前使用\fbox或将放入命令\fbox\textcolor或替换\fbox为来\fcolorbox更改框架的颜色:

\documentclass{article}

\usepackage{graphicx,trimclip,xcolor}

\begin{document}

\begin{figure}
    \centering
    \def\imagewidth{0.12\textwidth}%
    \fboxsep=0pt
    \includegraphics[width=\imagewidth]{example-image-a}\\
    \framebox[\imagewidth]{\clipbox{\fboxrule}{\includegraphics[width=
        \imagewidth]{example-image-a}}}\\
    \setlength{\fboxrule}{1ex}%
    \textcolor{green}{%
    \fbox{\clipbox{\fboxrule}{\includegraphics[width=
        \imagewidth]{example-image-a}}}}\\
    \fcolorbox{blue}{white}{\clipbox{\fboxrule}{\includegraphics[width=
      \imagewidth]{example-image-a}}}\\
    \caption{Caption}
    \label{fig:my_label}
\end{figure}

\end{document}

在此处输入图片描述

答案3

解决方案是使用 \textcolor 和 \frame。比 fbox 更好,因为它不会移动图形。

\documentclass{article}
\usepackage{tabularx}
\usepackage{booktabs}
\usepackage{graphicx}
\usepackage{amsmath}
\usepackage{float}
\usepackage{subfig}
\usepackage{tikz}% loads graphicx
\usepackage{graphicx,xcolor}

\begin{document}

\makeatletter
\newcommand{\definetrim}[2]{%
  \define@key{Gin}{#1}[]{\setkeys{Gin}{trim=#2,clip}}%
}
\makeatother

\definetrim{kit_mocap}{0 0 0 0} % {left bottom right top}
\def\barheight{0.015}
\def\imagewidth{0.12\textwidth}
\def\imagehspace{\hspace{-0.2em}}
\def\imagevspace{\vspace{-0.3cm}}
\setlength{\fboxrule}{1pt}
\setlength{\fboxsep}{0pt}

\begin{figure*}[ht]
\centering
\subfloat{
\begin{tikzpicture}
    \node[anchor=south west,inner sep=0] (image) at (0,0) {\textcolor{blue}{\frame{\includegraphics[width=\imagewidth,kit_mocap,clip]{example-image-a}}}};
\end{tikzpicture}}\imagehspace
\subfloat{
\begin{tikzpicture}
    \node[anchor=south west,inner sep=0] (image) at (0,0) {\includegraphics[width=\imagewidth,kit_mocap,clip]{example-image-a}};
\end{tikzpicture}}\imagehspace
\subfloat{
\begin{tikzpicture}
    \node[anchor=south west,inner sep=0] (image) at (0,0) {\includegraphics[width=\imagewidth,kit_mocap,clip]{example-image-a}};
\end{tikzpicture}}\imagehspace
\subfloat{
\begin{tikzpicture}
    \node[anchor=south west,inner sep=0] (image) at (0,0) {\includegraphics[width=\imagewidth,kit_mocap,clip]{example-image-a}};
\end{tikzpicture}}\imagehspace
\subfloat{
\begin{tikzpicture}
    \node[anchor=south west,inner sep=0] (image) at (0,0) {\includegraphics[width=\imagewidth,kit_mocap,clip]{example-image-a}};
\end{tikzpicture}}\imagehspace
\subfloat{
\begin{tikzpicture}
    \node[anchor=south west,inner sep=0] (image) at (0,0) {\includegraphics[width=\imagewidth,kit_mocap,clip]{example-image-a}};
\end{tikzpicture}}\imagehspace
\subfloat{
\begin{tikzpicture}
    \node[anchor=south west,inner sep=0] (image) at (0,0) {\includegraphics[width=\imagewidth,kit_mocap,clip]{example-image-a}};
\end{tikzpicture}}\imagehspace
\subfloat{
\begin{tikzpicture}
    \node[anchor=south west,inner sep=0] (image) at (0,0) {\includegraphics[width=\imagewidth,kit_mocap,clip]{example-image-a}};
\end{tikzpicture}}\imagevspace

\subfloat{
\begin{tikzpicture}[remember picture]
    \node[inner sep=0] (image1) at (0,0) {\includegraphics[width=\imagewidth,kit_mocap,clip]{example-image-a}};
\end{tikzpicture}}\imagehspace
\subfloat{
\begin{tikzpicture}[remember picture]
    \node[anchor=south west,inner sep=0] (image2) at (0,0) {\includegraphics[width=\imagewidth,kit_mocap,clip]{example-image-a}};
\end{tikzpicture}}\imagehspace
\subfloat{
\begin{tikzpicture}[remember picture]
    \node[inner sep=0] (image3) at (0,0) {\includegraphics[width=\imagewidth,kit_mocap,clip]{example-image-a}};
\end{tikzpicture}}\imagehspace
\subfloat{
\begin{tikzpicture}[remember picture]
    \node[anchor=south west,inner sep=0] (image4) at (0,0) {\includegraphics[width=\imagewidth,kit_mocap,clip]{example-image-a}};
\end{tikzpicture}}\imagehspace
\subfloat{
\begin{tikzpicture}[remember picture]
    \node[anchor=south west,inner sep=0] (image5) at (0,0) {\includegraphics[width=\imagewidth,kit_mocap,clip]{example-image-a}};
\end{tikzpicture}}\imagehspace
\subfloat{
\begin{tikzpicture}[remember picture]
    \node[anchor=south west,inner sep=0] (image6) at (0,0) {\includegraphics[width=\imagewidth,kit_mocap,clip]{example-image-a}};
\end{tikzpicture}}\imagehspace
\subfloat{
\begin{tikzpicture}[remember picture]
    \node[anchor=south west,inner sep=0] (image7) at (0,0) {\includegraphics[width=\imagewidth,kit_mocap,clip]{example-image-a}};
\end{tikzpicture}}\imagehspace
\subfloat{
\begin{tikzpicture}[remember picture]
    \node[anchor=south west,inner sep=0] (image8) at (0,0) {\includegraphics[width=\imagewidth,kit_mocap,clip]{example-image-a}};
\end{tikzpicture}}\imagevspace

\subfloat{
\begin{tikzpicture}[remember picture]
    \node[inner sep=0] (image1) at (0,0) {\fbox{\includegraphics[width=\imagewidth,clip]{example-image-a}}};
\end{tikzpicture}}\imagehspace
\subfloat{
\begin{tikzpicture}[remember picture]
    \node[anchor=south west,inner sep=0] (image2) at (0,0) {\fbox{\includegraphics[width=\imagewidth,clip]{example-image-a}}};
\end{tikzpicture}}\imagehspace
\subfloat{
\begin{tikzpicture}[remember picture]
    \node[inner sep=0] (image3) at (0,0) {\includegraphics[width=\imagewidth,clip]{example-image-a}};
\end{tikzpicture}}\imagehspace
\subfloat{
\begin{tikzpicture}[remember picture]
    \node[anchor=south west,inner sep=0] (image4) at (0,0) {\includegraphics[width=\imagewidth,clip]{example-image-a}};
\end{tikzpicture}}\imagehspace
\subfloat{
\begin{tikzpicture}[remember picture]
    \node[anchor=south west,inner sep=0] (image5) at (0,0) {\includegraphics[width=\imagewidth,clip]{example-image-a}};
\end{tikzpicture}}\imagehspace
\subfloat{
\begin{tikzpicture}[remember picture]
    \node[anchor=south west,inner sep=0] (image6) at (0,0) {\includegraphics[width=\imagewidth,clip]{example-image-a}};
\end{tikzpicture}}\imagehspace
\subfloat{
\begin{tikzpicture}[remember picture]
    \node[anchor=south west,inner sep=0] (image7) at (0,0) {\includegraphics[width=\imagewidth,clip]{example-image-a}};
\end{tikzpicture}}\imagehspace
\subfloat{
\begin{tikzpicture}[remember picture]
    \node[anchor=south west,inner sep=0] (image8) at (0,0) {\includegraphics[width=\imagewidth,clip]{example-image-a}};
\end{tikzpicture}}

\end{figure*}

\end{document}

在此处输入图片描述

相关内容