0

Consider an image with:

  • a caption of unknown length
  • unknown height
  • unknown width
  • unknown aspect ratio

I'd like this image to take up the full width of the page, unless the combined height of the image and its caption is taller than the page, in which case I'd like the combined height of the image and caption to max out at the height of the page.

If we ignore the caption, this can be achieved with:

\includegraphics[width=\textwidth,height=\textheight,keepaspectratio]{image.jpg}

But obviously that doesn't consider the caption. Is this achievable? Are there any sneaky packages that already do this sort of witchcraft?

NB: There's a similar question here, but they know the length of their caption

  • Can you make a minimal working example (MWE)? Also if you speak of "height of page" do you mean \textheight? – user36296 Oct 5 '18 at 19:56
  • That includegraphics is my current MWE (give or take a \document). I do indeed mean \textheight (and \textwidth) – Alec Oct 5 '18 at 19:59
  • That's a lot of unknowns and not very much guidance. Particularly since you've not even provided a MWE. This feels quite open to interpretation. – A.Ellett Oct 6 '18 at 6:43
1

This solution is my best guess for what you're talking about. Still I did need to make so assumptions. But perhaps I'm off base.

I start of by presuming you know the dimensions of the page that you will be printing to. If you don't know even that much,.... well, you need to give us more of a helping hand to understand what you're doing.

Each of the following images were created using the same environment. I've loaded the geometry package with showframe so you can see how the entire page is filled more easily.

enter image description here

enter image description here

enter image description here

This is the code I used to create the above pages.

\documentclass{article}

\usepackage[showframe,margin=0.5in]{geometry}
\usepackage{graphicx}
\usepackage{mwe}

\newsavebox\aebox
\newenvironment{captionedpicture}[2][5in]
  {\def\mypic{#2}%%
   \begin{lrbox}\aebox
    \begin{minipage}[b]{#1}
      \raggedright}
  {\end{minipage}%%
   \end{lrbox}%%
   \noindent\begin{minipage}[t]{\columnwidth}
      \hspace*{\fill}%%
      \includegraphics[height=\dimexpr\textheight-\ht\aebox-2ex]{\mypic}%%
      \hspace*{\fill}%%
      \par\addvspace{1ex}%%
      \hspace*{\fill}\usebox{\aebox}\hspace*{\fill}
   \end{minipage}}

\pagestyle{empty}

\begin{document}

\begin{captionedpicture}{example-image-10x16}
  {\LARGE\centering A HUGE CAPTION\par}
  ARMA virumque cano, Troiae qui primus ab oris
  Italiam, fato profugus, Laviniaque venit
  litora, multum ille et terris iactatus et alto
  vi superum saevae memorem Iunonis ob iram;
  multa quoque et bello passus, dum conderet urbem,
  inferretque deos Latio, genus unde Latinum,
  Albanique patres, atque altae moenia Romae.

  Musa, mihi causas memora, quo numine laeso,
  quidve dolens, regina deum tot volvere casus
  insignem pietate virum, tot adire labores
  impulerit.  Tantaene animis caelestibus irae?

  Urbs antiqua fuit, Tyrii tenuere coloni,
  Karthago, Italiam contra Tiberinaque longe
  ostia, dives opum studiisque asperrima belli;
  quam Iuno fertur terris magis omnibus unam
  posthabita coluisse Samo; hic illius arma,
  hic currus fuit; hoc regnum dea gentibus esse,
  si qua fata sinant, iam tum tenditque fovetque.
  Progeniem sed enim Troiano a sanguine duci
  audierat, Tyrias olim quae verteret arces;
  hinc populum late regem belloque superbum
  venturum excidio Libyae:  sic volvere Parcas.
  Id metuens, veterisque memor Saturnia belli,
  prima quod ad Troiam pro caris gesserat Argis---
  necdum etiam causae irarum saevique dolores
  exciderant animo:  manet alta mente repostum
  iudicium Paridis spretaeque iniuria formae,
  et genus invisum, et rapti Ganymedis honores.
  His accensa super, iactatos aequore toto
  Troas, reliquias Danaum atque immitis Achilli,
  arcebat longe Latio, multosque per annos
  errabant, acti fatis, maria omnia circum.
  Tantae molis erat Romanam condere gentem!

  Vix e conspectu Siculae telluris in altum
  vela dabant laeti, et spumas salis aere ruebant,
  cum Iuno, aeternum servans sub pectore volnus,
  haec secum:  'Mene incepto desistere victam,
  nec posse Italia Teucrorum avertere regem?
  Quippe vetor fatis.  Pallasne exurere classem
  Argivom atque ipsos potuit submergere ponto,
  unius ob noxam et furias Aiacis Oilei?
  Ipsa, Iovis rapidum iaculata e nubibus ignem,
  disiecitque rates evertitque aequora ventis,
  illum expirantem transfixo pectore flammas
  turbine corripuit scopuloque infixit acuto.
  Ast ego, quae divom incedo regina, Iovisque
  et soror et coniunx, una cum gente tot annos
  bella gero! Et quisquam numen Iunonis adoret
  praeterea, aut supplex aris imponet honorem?'
\end{captionedpicture}%%

\clearpage

\begin{captionedpicture}{example-image-10x16}
  {\LARGE\centering A modest caption\par}
  ARMA virumque cano, Troiae qui primus ab oris
  Italiam, fato profugus, Laviniaque venit
  litora, multum ille et terris iactatus et alto
  vi superum saevae memorem Iunonis ob iram;
  multa quoque et bello passus, dum conderet urbem,
  inferretque deos Latio, genus unde Latinum,
  Albanique patres, atque altae moenia Romae.

  Musa, mihi causas memora, quo numine laeso,
  quidve dolens, regina deum tot volvere casus
  insignem pietate virum, tot adire labores
  impulerit.  Tantaene animis caelestibus irae?
\end{captionedpicture}%%

\clearpage

\begin{captionedpicture}{example-image-10x16}
  {\LARGE\centering A one liner\par}
\end{captionedpicture}%%

\end{document}

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.