3

How can I have a red frame that expands inwards? For example, the code below generates a frame that expands outwards. How can I have it expand inwards so it doesnt take any extra space beyond the figure? For example \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}

enter image description here

3 Answers 3

5

There are funny interactions between \fbox and \includegraphics, so my solution is to typeset the graphics with zero width and an \fbox with the appropriate dimensions.

\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}

I used 0.4mm just to make the frame more visible.

enter image description here

You can add a trailing color optional argument, if you want colored frames.

\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}

enter image description here

2
  • may i know how to change the colors? i need 2 different colors so I can create 2 variables \frameincludegraphics1 and \frameincludegraphics2 but how do i change the colors for these boxes? I tried using fcolorbox{red}{white} but the second argument always paints over my figure.
    – Kong
    Apr 27, 2023 at 16:06
  • @Kong Added colors
    – egreg
    Apr 27, 2023 at 16:20
3

If you use a \frame, \fbox, or \framebox, the problem would be, that some of the rules are drawn, before the content. So you would have to trim the content to avoid, that it is dawn above the already drawn rule. Another suggestion would be, to first draw the image and then draw a frame with the wanted size:

\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}

enter image description here

Alternatively you can clip the content by the rule width using package 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}

enter image description here

You can additionally use \color before the \fbox or put the \fbox into a \textcolor command or replace \fbox by \fcolorbox to change the color of the frame:

\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}

enter image description here

2

Solution is to use \textcolor and \frame. Better than fbox since it doesnt push the figures around.

\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}

enter image description here

2
  • 1
    Any reason why you're using a tikzpicture for each image rather than just setting them using \includegraphics? Seems completely unnecessary to have all that code that achieves the same thing.
    – Werner
    Apr 27, 2023 at 16:06
  • \frame does not really expand the frame inwards. And it also does not use \fboxrule. If you already use tikzpicture, why not simply use a background rectangle with different colors (and even white)? Was your real question about how to get such a frame around tikzpicture with TikZ? Than you should ask this and not showing a cryptic \frame command and an example, that does not even use TikZ.
    – cabohah
    Apr 28, 2023 at 6:55

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .