Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

I have got two adjustboxes (from the adjustbox package). Now I need to give the second box the same totalheight as the one which the first box got.

\documentclass{article}
\usepackage{etex}
\usepackage[etex=true,export]{adjustbox}
\begin{document}

\begin{adjustbox}{width={\textwidth},totalheight={8cm},keepaspectratio}
...some content ...
\end{adjustbox}

(In between is some text and a page break.)

\begin{adjustbox}{totalheight={<the same as in the first adjustbox>},keepaspectratio}
...other content...
\end{adjustbox}

\end{document}

I can't use the 8cm (in the real document there is another variable instead of a fixed value), because the height might be smaller if the box needed to be scaled down because it was too wide. Adjustbox says that \totalheight would contain this height, but how do I export this to be re-used in the second box?

share|improve this question

2 Answers 2

up vote 6 down vote accepted

There are no key options yet to store the size of an adjustbox, but I was already thinking to add them. The value of \totalheight can only be used in keys which affect the size. These actually box the content and therefore update this length to have the correct value. You can however do the required boxing yourself using some small code added using the precode key, by using features of the collectbox package, which is internally used by adjustbox anyway. You need a length register which is assigned globally.

\documentclass{article}
\usepackage{etex}
\usepackage[etex=true,export]{adjustbox}
\newlength\mylength
\begin{document}

\begin{adjustbox}{width={\textwidth},totalheight={8cm},keepaspectratio,precode={\collectbox{\global\setlength\mylength{\totalheight}\BOXCONTENT}}}
  ... some content ...
\end{adjustbox}

(In between is some text and a page break.)

\begin{adjustbox}{totalheight=\mylength,keepaspectratio}
 ...other content...
\end{adjustbox}

\end{document}

If you need this more often then you can define a new key for this:

\documentclass{article}
\usepackage{etex}
\usepackage[etex=true,export]{adjustbox}
\newlength\mylength

\makeatletter
\define@key{adjbox}{settototalheight}[1]{%
    \Gin@esetsize
    \@tempswatrue
    \adjust@addcode{\@collectbox{\global#1=\totalheight\relax\BOXCONTENT}}{}%
}
\makeatother

\begin{document}

\begin{adjustbox}{width={\textwidth},totalheight={8cm},keepaspectratio,settototalheight=\mylength}
  ... some content ...
\end{adjustbox}

(In between is some text and a page break.)

\begin{adjustbox}{totalheight=\mylength,keepaspectratio}
 ...other content...
\end{adjustbox}

\end{document}

Note that you need to add the key at the very end, because the order is significant.

share|improve this answer
    
It works (as your answers always do), but I was really surprised that \fitsinglefig...\captionsetup...\begin{table}[H]\begin{leftfullpage}\raggedleft‌​\makeatletter\begin{adjustbox}{width={\textwidth},totalheight={\fitfigheight},kee‌​paspectratio,precode={\collectbox{\global\setlength\@tempdimc{\totalheight}\BOXCO‌​NTENT\xdef\ajbStotalheight{\@tempdimc}}}}\begin{tabular}...<end everything, new page>\noindent\captionsetup...\begin{table}[H]\begin{fullpage}\begin{adjustbox}{‌​valign=b,totalheight={\ajbStotalheight},keepaspectratio}\begin{tabular}... worked without any further problem! +1 and accepted. –  Stephen Apr 10 '12 at 15:29
    
Note that \xdef\ajbStotalheight{\@tempdimc} doesn't make much sense. Dimensions aren't expandable. Either write \the\@tempdimc instead of \@tempdimc to expand to its current value, or directly \xdef\ajbStotalheight{\the\totalheight}, before or after \BOXCONTENT. –  Martin Scharrer Apr 10 '12 at 15:40
    
The limited number of characters of a comment and the wrong place to shorten the code... - thanks for pointing it out! –  Stephen Apr 10 '12 at 15:51
1  
@Stephen: I just added a key like this to adjustbox, but with a different name. It will be in the next release. –  Martin Scharrer May 16 '12 at 0:25

The only way I see is to set the first box in a storage bin and measure it

% in the preamble
\newsavebox{\stephenbox}
\newlength{\stephenlength}

% in the document
\sbox{\stephenbox}
  {\begin{adjustbox}{width={\textwidth},totalheight={8cm},keepaspectratio}
   ...some content ...
   \end{adjustbox}}%
\setlength{\stephenlength}{\ht\stephenbox}%
\addtolength{\stephenlength}{\dp\stephenbox}%
\usebox{\stephenbox}

some lines later

\begin{adjustbox}{totalheight=\stephenlength,keepaspectratio}
...other content...
\end{adjustbox}
share|improve this answer
    
While your solution generally works (+1), I had hoped for an "in-adjustbox-package-solution" instead of boxing and measuring it. Martin Scharrer provided that. Could have send an e-mail to him, because he is the author/maintainer of the adjustbox package, but his solution here could also help other people with a similar problem. - \end{leftfullpage} resets \stephenlength to 0pt, but I did not mention in my question that the adjustbox is inside a \makeatletter inside \raggedleft inside \begin{leftfullpage} inside \begin{table}[H] inside \captionsetup inside \fitsinglefig... –  Stephen Apr 10 '12 at 15:35
    
@Stephen While my hack works, a solution based on adjustbox keywords is surely better. –  egreg Apr 10 '12 at 15:38

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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