2

I am a newbie in LaTeX. I am currently using it writing a very simple school lab report. I encounter a problem when I try to enlarge the inserted pictures by changing the

[width=6.0in]

parameter in

The graph now becomes Figure~\ref{pmos_gamma}.
\begin{figure}
    \centering
    \includegraphics[width=6.0in]{pmos_gamma}
    \caption{$V_{BS}=0.1V, 0.3V, 0.5V, 0.7V$}
    \label{pmos_gamma}
\end{figure}

I cannot know the maximum size of a picture that will not make itself "pushed" to the next page. If the picture is too big, as we know it will be pushed down to next page. I don't wish that to happen.

So how can I ensure the picture will NOT be pushed to the next page while I am trying to enlarge the picture?

(Position highest priority. Under that the picture should be as large as possible.)

Update:

Adopting the \MyFig{}{}{} solution, the following code snippet

\title{blabla}
\author{
blabla
}
\date{\today}

\documentclass[12pt]{article}
\usepackage[margin=1in]{geometry}
\usepackage{graphicx} % for graphics
\usepackage{capt-of}
\linespread{1.25}

\newcommand\MyFig[3]{%
    \par\medskip\noindent%
    \setbox0=\vbox{\captionof{figure}{#1}}%
    \begin{minipage}{\textwidth}
    \centering
    \includegraphics[%
        height=\dimexpr\pagegoal-\pagetotal-\abovecaptionskip-\ht0-1\bigskipamount-\medskipamount\relax,
        width=\textwidth,keepaspectratio]{#1}
    \captionof{figure}{#2}
    \label{#3}%
    \end{minipage}\par\bigskip%
}

\begin{document}
\maketitle

\begin{abstract}
blabla
\end{abstract}

\section{Introduction}
blabla

\paragraph{Outline}
blabla

\section{Task 1}\label{task1}
\paragraph{2.1 $V_{t0}$}
After the circuit has been set up in \textit{LTSpice} as instructed, the simulations were run. The family of $I-V$ curves generated are shown in Figure~\ref{nmosfigure}.
\MyFig{task1_nmos}{NMOS $I_{DS}$ vs. $V_{DS}$}{nmosfigure}

\end{document}

produces the following errors:

! Missing $ inserted.
<inserted text>
$
l.47 ...s}{NMOS $I_{DS}$ vs. $V_{DS}$}{nmosfigure}
I've inserted a begin-math/end-math symbol since I think
you left one out. Proceed, with fingers crossed.
! Extra }, or forgotten $.
\sbox ...hbox {\color@setgroup #2\color@endgroup }
l.47 ...s}{NMOS $I_{DS}$ vs. $V_{DS}$}{nmosfigure}
I've deleted a group-closing symbol because it seems to be
spurious, as in `$x}$'. But perhaps the } is legitimate and
you forgot something else, as in `\hbox{$x}'. In such cases
the way to recover is to insert both the forgotten and the
deleted material, e.g., by typing `I$}'.
! Missing $ inserted.
<inserted text>
$
l.47 ...s}{NMOS $I_{DS}$ vs. $V_{DS}$}{nmosfigure}
I've inserted a begin-math/end-math symbol since I think
you left one out. Proceed, with fingers crossed.
! Missing } inserted.
<inserted text>
}
l.47 ...s}{NMOS $I_{DS}$ vs. $V_{DS}$}{nmosfigure}
I've inserted something that you may have forgotten.
(See the <inserted text> above.)
With luck, this will get me unwedged. But if you
really didn't forget anything, try typing `2' now; then
my insertion and my current dilemma will both disappear.
! I can't write on file `report.pdf'.
Please type another file name for output
! Emergency stop.
<argument> ...stobj \GPT@objref }}}\fi \pdfximage
\GPT@RuleAttr \ifx \GPT@pa...
l.47 ...s}{NMOS $I_{DS}$ vs. $V_{DS}$}{nmosfigure}
*** (job aborted, file error in nonstop mode)
Here is how much of TeX's memory you used:
1654 strings out of 494045
22800 string characters out of 3148364
80363 words of memory out of 3000000
4970 multiletter control sequences out of 15000+200000
9456 words of font info for 33 fonts, out of 3000000 for 9000
715 hyphenation exceptions out of 8191
27i,6n,32p,370b,199s stack positions out of 5000i,500n,10000p,200000b,50000s
! ==> Fatal error occurred, no output PDF file produced!

Line 47 is \MyFig{task1_nmos}{NMOS $I_{DS}$ vs. $V_{DS}$}{nmosfigure}

2 Answers 2

2

Update:

Now the caption is previously boxed, its height measured and then taken into account automatically; the syntax for \MyFig is simply

\MyFig{<image file>}{<caption>}{<label>}

The code:

\documentclass{article}
\usepackage{graphicx}
\usepackage{lipsum}
\usepackage{capt-of}

\newcommand\MyFig[3]{%
\par\medskip\noindent%
\setbox0=\vbox{\captionof{figure}{#2}}%
\begin{minipage}{\textwidth}
\centering
\includegraphics[%
  height=\dimexpr\pagegoal-\pagetotal-\abovecaptionskip-\ht0-1\bigskipamount-\medskipamount\relax,
  width=\textwidth,keepaspectratio]{#1}
\captionof{figure}{#2}
\label{#3}%
\end{minipage}\par\bigskip%
}

\begin{document}

\lipsum[1-3]

\MyFig{example-image-a}{adasf sadgsdg sdfgsf sadgsdg sdfgsf sadgsdg sdfgsf sadgsdg sdfgsf sadgsdg sdfgsf sadgsdg sdfgsf sadgsdg sdfgsf sadgsdg sdfgsf sadgsdg sdfgsf sadgsdg sdfgsf sadgsdg sdfgsf sadgsdg sdfgsf sadgsdg sdfgsf}{fig:testa}

\lipsum[1]

\MyFig{example-image-b}{adasf sadgsdg sdfgsf sadgsdg sdfgsf sadgsdg sdfgsf sadgsdg sdfgsf sadgsdg sdfgsf sadgsdg sdfgsf sadgsdg sdfgsf sadgs}{fig:testb}

\lipsum[1-5]

\MyFig{example-image-c}{adasf sadgsdg sdfgsf}{fig:testc}

\end{document}

enter image description here

\MyFig scales the figure accordingly to the available space on the page. It uses a minipage and \captionof from the capt-of package to provide a caption.

10
  • But that still needs the caption to be manually adjusted? Does it? If that is what you meant by, taking care of the caption height.
    – Masroor
    Commented Sep 10, 2013 at 3:45
  • Thanks! It seems promising. As a newbie, I find it even difficult to apply this to my code. May I ask whether you could apply this to my code given in the question? Just one demo on my code will do. Thanks! Commented Sep 10, 2013 at 3:46
  • @MMA you need to give as optional argument the number of lines spanned by the caption. Commented Sep 10, 2013 at 3:47
  • @perfectionm1ng my example code shows how to use the macro. Copy the definition of \MyFig in the preamble of your document and then, instead of \begin{figure} \centering \includegraphics[width=\textwidth]{pmos_gamma} \caption{$V_{BS}=0.1V, 0.3V, 0.5V, 0.7V$} \label{pmos_gamma} \end{figure} simply use \MyFig{pmos_gamma}{$V_{BS}=0.1V, 0.3V, 0.5V, 0.7V$}{pmos_gamma} (no need for the optional argument here, since the caption only spans one line). Commented Sep 10, 2013 at 3:49
  • @MMA now, in my updated answer, no manual calculations are needed. Commented Sep 10, 2013 at 4:12
2

Why don't you modify your code like this so that the width of the picture is dependent on text width?

\begin{figure}
    \centering
    \includegraphics[width=\textwidth]{pmos_gamma}
    \caption{$V_{BS}=0.1V, 0.3V, 0.5V, 0.7V$}
    \label{pmos_gamma}
\end{figure}

If you still get some overfull message and/or unwanted situation you will want to modify the width to something like width=0.9\textwidth.

The above answer is applicable if your picture is more of a landscape nature (wider than longer). If your picture is more of portrait nature, then something like \includegraphics[width=\textheight]{pmos_gamma} or \includegraphics[width=0.8\textheight]{pmos_gamma} will be applicable.

Please tweak a bit with the parameters to get the best results.

Of course, you can write a macro to measure the dimensions beforehand and then use some kind of if-then to dynamically apply the dimensions. But that will more of an overkill, IMHO.

2
  • Yes, it helps a bit. But I still have to adjust the width=\textwidth so as to keep all the pictures in position right? E.g. some big pictures maybe 0.5\textwidth. small ones \textwidth will do. Can I somewhat fix the locations first, and then enlarge the pic to its max? Commented Sep 10, 2013 at 3:16
  • 1
    If you mean placing at your desired/intended location by "fix the locations first", then perhaps you can use the float package and use [H] as placement specifier. And please see the end part of my post about writing a macro to dynamically adjust the width. As my experience goes over these years, I attempted that many times, only to give up finding that there is just one more special case to handle. Also please see this post for other relevant discussions.
    – Masroor
    Commented Sep 10, 2013 at 3:29

You must log in to answer this question.

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