3

I'm new to LaTeX. I'm using a template created by another person and I'd like to make a modification. The template uses a subfunction to create figures and I'd like to add a border to all images.

I currently insert a figure like this:

\munepsfig[width=5in]{CG289}{CG289}

The munepsfig is a sub function created in a .sty file. The sub function looks like this:

\newcommand{\munepsfig}[3][scale=1.0]{%
    \begin{figure}[!htbp]
        \centering
        \vspace{2mm}
        \includegraphics[#1]{figures/#2.png}
        \caption{#3}
        \label{fig:#2}
    \end{figure}
}

I've tried a number of suggestions from the internet but nothing seems to show up - the figure always shows with no change or the script fails. I'm just looking for a black border of a thickness I can choose.

0
5

To get a border for an image you can use the command \framebox{}. To define the width of the rule you can use command \setlength{\fboxrule}{0.5pt}.

Both combined leads to the following

\newcommand{\munepsfig}[4][scale=1.0]{% <===============================
    \begin{figure}[!htbp]
        \centering
        \vspace{2mm}
        \setlength{\fboxrule}{#4} % <===================================
        \framebox{\includegraphics[#1]{#2.png}} % <=====================
        \caption{#3}
        \label{fig:#2}
    \end{figure}
}

Now you can use the command

%\munepsfig[option]{figure}{caption}{fboxrule}
\munepsfig[width=5cm]{example-image}{caption of figure}{1pt}

to print the image example-image with an border of 1pt.

The complete MWE

\documentclass{article}

\usepackage{graphicx}

\newcommand{\munepsfig}[4][scale=1.0]{% <===============================
    \begin{figure}[!htbp]
        \centering
        \vspace{2mm}
        \setlength{\fboxrule}{#4} % <===================================
        \framebox{\includegraphics[#1]{#2.png}} % <=====================
        \caption{#3}
        \label{fig:#2}
    \end{figure}
}

\begin{document}

%\munepsfig[option]{figure}{caption}{fboxrule}
\munepsfig[width=5cm]{example-image}{caption of figure}{1pt}

\munepsfig[width=5cm]{example-image-a}{caption of figure2}{0.5pt}

\end{document}

gives you:

resulting images

To get rid of the space between image and rule use command \setlength{\fboxsep}{0pt}. See the following new MWE

\documentclass{article}

\usepackage{graphicx}

\newcommand{\munepsfig}[4][scale=1.0]{% <===============================
    \begin{figure}[!htbp]
        \centering
        \vspace{2mm}
        \setlength{\fboxrule}{#4} % <===================================
        \setlength{\fboxsep}{0pt} % <===================================
        \framebox{\includegraphics[#1]{#2.png}} % <=====================
        \caption{#3}
        \label{fig:#2}
    \end{figure}
}

\begin{document}

%\munepsfig[option]{figure}{caption}{fboxrule}
\munepsfig[width=5cm]{example-image}{caption of figure}{1pt}

\munepsfig[width=5cm]{example-image-a}{caption of figure2}{0.5pt}
\end{document}

resulting in the pdf:

enter image description here

1
  • Thanks, that worked! Is there any way I get rid of the white space inside of the black border? Nov 11 '17 at 12:19
2

With pstricks it is fairly easy to achieve this.

 \documentclass{article}
 \usepackage{pstricks}
 \usepackage{graphicx}
 \newcommand{\munepsfig}[4][scale=1.0]{%
     \begin{figure}[!htbp]
         \centering
         \vspace{2mm}
         \psframebox[linewidth=#4]{\includegraphics[#1]{figures/#2.eps}}
         \caption{#3}
         \label{fig:#2}
     \end{figure}
 }

 \begin{document}
 \munepsfig[scale=0.5]{MarmotOutlook}{Outlook.}{2pt}

 \end{document}

EDIT: Now the same thing with TiKz:

 \documentclass{article}
 \usepackage{tikz}
 \usepackage{graphicx}
 \newcommand{\munepsfig}[4][scale=1.0]{%
     \begin{figure}[!htbp]
         \centering
         \vspace{2mm}
         \tikz\node[draw=black,line width=#4](#2){\includegraphics[#1]{figures/#2.png}};
         \caption{#3}
         \label{fig:#2}
     \end{figure}
 }

 \begin{document}
 \munepsfig[scale=0.5]{MarmotOutlook}{Outlook.}{2pt}

 \end{document}
2
  • I'd like to give this a shot, but my inexperience in latex is probably preventing me. I've placed the \usepackage{pstricks} command in both the main document just below \begin{document} and in the .sty file. I get an error pstricks.sty not found, so I'm guessing this wasn't included in my initial installation. I've found a version of it here: ctan.math.utah.edu/ctan/tex-archive/graphics/pstricks/base/… and I place that with all the other files, but then it says pstricks.tex not found. Am I going down the wrong rabbit hole? Thanks! Nov 11 '17 at 12:23
  • @TrevorHarris : You cannot put a \usepackage command after \begin{document}. And of course you must have pstricks installed, but as far as I know it is part of any standard TeX installation, so I am surprised to hear about the error. Further, if you load a package in another package, you may better use RequirePackage.
    – user121799
    Nov 11 '17 at 15:35

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.