1
    \documentclass[12pt]{exam}
    \newtheorem{theorem}{Theorem}
    \usepackage{graphicx}
    \usepackage{amsmath}
    \usepackage{commath}
    \usepackage{epstopdf}
    \usepackage{tikzpagenodes}
    \usepackage{float}
    \begin{document}
    \usepackage{float}

\usepackage{lipsum}

\newenvironment{absolutelynopagebreak}
  {\par\nobreak\vfil\penalty0\vfilneg
   \vtop\bgroup}
  {\par\xdef\tpd{\the\prevdepth}\egroup
   \prevdepth=\tpd}

   \begin{questions}
   \question Answer the following questions on the basis of given diagram
   \begin{figure}[hbt!]
   \begin{center}
   \includegraphics[scale=0.70]{images/assignment-1.png}
   \end{center}
   \end{figure}

   \end{questions}
   \end{document}

I am trying to frame a picture based function but the picture is going down in the next page, besides that picture is shifted towards right, how to bring it back in the current page, in the center position and just near to the place where the question number is labelled.

Here is my updated code as per the suggestions by @ABlueChameleon

\documentclass[12pt]{exam}
        \newtheorem{theorem}{Theorem}
        \usepackage{graphicx}
        \usepackage{amsmath}
        \usepackage{commath}
        \usepackage{epstopdf}
        \usepackage{tikzpagenodes}
        \usepackage{float}
        \begin{document}

       \begin{questions}
       \question Answer the following questions on the basis of given diagram
       \begin{figure}[hbt!]
       \begin{absolutelynopagebreak}
   \includegraphics[scale=0.70]{images/assignment-1.png}
   \end{absolutelynopagebreak}

       \end{figure}

       \end{questions}
       \end{document}

1 Answers1

1

If you do not the image before of the question, or in the next page, do not use the t (top) option.

As you are using also the ! option, changes are that your image must be still too big for the available space. The option scale is not the best one to fit an image to a specific document layout because produces a size relative to the original image but not relative to the available space in the page. For that you should use width and/or height.

When you use only one of these options, the image will be scaled proportionally. Using both width and height obviously the image will be distorted to accomplish both dimensions, unless you use keepaspectratio, that have an evident meaning. In that case the image will be scaled as munch as possible, but without exceeding that settings.

As you have only a line of text, this should work also with your image:

\documentclass[12pt]{exam}
\usepackage{graphicx}
\usepackage{commath}
\begin{document}
\begin{questions}
\question Answer the following questions on the basis of given diagram
\begin{figure}[h!]
\centering
\includegraphics[
        width=\linewidth,
        height=\dimexpr\textheight-2\baselineskip,
        keepaspectratio
                ]{example-image-9x16.png}
\end{figure}
\end{questions}
\end{document}

mwe

Otherwise, reduce the height as needed. For example to \dimexpr\textheight-5\baselineskip two allow a few lines of text in teh question, or simply .33\textheight. to take ony a third of the text area.

On the other hand, if the image is still shifted towards any direction mean that there some unequal blank spaces around the graph in the source image. If you put the image in a \fbox (e.g. \fbox{\includegraphics[...]{...}}) you will see that the whole image, including the invisible backhround, is correctly placed. In that case, see How to crop background from image automatically?. You can even do that with respect relative coordinates options) ...or you can simply crop the source image with some editor of images, like Gimp.

Fran
  • 80,769