# How to set border for figures

When I insert figures into my tex,sometimes it goes out of the page. Can you please suggest a method to avoid this.

-
Welcome to TeX.SX! –  Papiro Feb 3 at 14:51

adjustbox is your friend. Load adjustbox with export option:

\usepackage[export]{adjustbox}


And add max width=\linewidth in the options of \includegraphics. The images will be scaled only if they are broader than the \textwidth/\linewidth (as the case may be)

\documentclass{article}
\usepackage{showframe}   %% just for demo
\begin{document}
\noindent
\includegraphics[width=2\textwidth,,max width=\linewidth]{example-image-a}

\noindent
\includegraphics[width=0.5\textwidth,,max width=\linewidth]{example-image-a}
\end{document}


You can make a macro if you like:

\let\oldincludegraphics\includegraphics
\renewcommand{\includegraphics}[2][]{%
\oldincludegraphics[#1,max width=\linewidth]{#2}}


Now you can use

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


as usual.

-