[Tex/LaTex] How to scale a scheme in chemfig, using the figure environment

chemfigscaling

I am trying to scale a reaction scheme generated with chemfig to .9/textwidth. Below is my MWE.

\documentclass{report}
\usepackage{chemfig}
\usepackage[version=3]{mhchem}
\begin{document}

\begin{figure}[width=.9\textwidth]
\schemestart
\ce{O2^.-}\arrow (c1--c2){0}\ce{NO^.}\merge{v}(c1)(c2)--(c3)\ce{ONOO-}
\arrow (--c4){<=>[*{0}\parbox{1.2cm}{\ce{+ H+}\\ pK 6.6}][*{0}\ce{- H+}]}[-90,1.5]\ce{ONOOH}
\arrow(--c5){->[\ce{+ NO^.}][\ce{- HNO2}]}[0,1.5]\ce{NO2^.}
\arrow(--c6){->[\ce{+ NO^.}]}\ce{N2O3}
\arrow(@c4--c7){->[\ce{+ O2^.-}][\ce{- OH-},\ce{- O2}]}[180,1.5]\ce{NO2^.}
\arrow(@c7--c8){->[\ce{+ O2^.-}]}[180]\parbox{1.5cm}{\centering \ce{O2NOO-}\\or \\\ce{NO2- + O2}}
\arrow(@c3--@c7){->[\ce{+ CO2}][\ce{- CO3^.}]}[2.12]
\arrow(@c3--@c5){->[\ce{+ CO2}][\ce{- CO3^.-}]}[2.12]
\arrow(@c1--c9){->[\ce{+ O2^.-}, \ce{+ 2H+}][\ce{- O2}]}[180,1.5]\ce{H2O2}
\arrow(@c9--c10){->[\ce{Fe2+}][\ce{- OH-}]}[180]\ce{^.OH}
\arrow(@c2--c11){->[\ce{+ O2}]}$[$\ce{^.ONOO}$]$
\arrow(@c11--c12){->[\ce{+ ^.NO}]}\ce{2 NO2^.}
\schemestop

\end{figure}
\end{document}

However, this still results in the following and seems not to scale the figure:

Overfull \hbox (71.11223pt too wide) in paragraph at lines 20--21.

Any thoughts and comments how to change this would be very much appreciated. Hope that is clear enough.

Best Answer

Just put it in a \scalebox perhaps:

\documentclass{report}
\usepackage{chemfig}
\usepackage{graphicx}
\usepackage[version=3]{mhchem}
\begin{document}

\begin{figure}[width=.9\textwidth]
\scalebox{.6}{
\schemestart
\ce{O2^.-}\arrow (c1--c2){0}\ce{NO^.}\merge{v}(c1)(c2)--(c3)\ce{ONOO-}
\arrow (--c4){<=>[*{0}\parbox{1.2cm}{\ce{+ H+}\\ pK 6.6}][*{0}\ce{- H+}]}[-90,1.5]\ce{ONOOH}
\arrow(--c5){->[\ce{+ NO^.}][\ce{- HNO2}]}[0,1.5]\ce{NO2^.}
\arrow(--c6){->[\ce{+ NO^.}]}\ce{N2O3}
\arrow(@c4--c7){->[\ce{+ O2^.-}][\ce{- OH-},\ce{- O2}]}[180,1.5]\ce{NO2^.}
\arrow(@c7--c8){->[\ce{+ O2^.-}]}[180]\parbox{1.5cm}{\centering \ce{O2NOO-}\\or \\\ce{NO2- + O2}}
\arrow(@c3--@c7){->[\ce{+ CO2}][\ce{- CO3^.}]}[2.12]
\arrow(@c3--@c5){->[\ce{+ CO2}][\ce{- CO3^.-}]}[2.12]
\arrow(@c1--c9){->[\ce{+ O2^.-}, \ce{+ 2H+}][\ce{- O2}]}[180,1.5]\ce{H2O2}
\arrow(@c9--c10){->[\ce{Fe2+}][\ce{- OH-}]}[180]\ce{^.OH}
\arrow(@c2--c11){->[\ce{+ O2}]}$[$\ce{^.ONOO}$]$
\arrow(@c11--c12){->[\ce{+ ^.NO}]}\ce{2 NO2^.}
\schemestop
}

\end{figure}
\end{document}

However, to answer the more specific question of matching the figure width to the textwidth, we can borrow from Philipp's answer at How to find the ratio of a length command (e.g., \textwidth) to a reference value (e.g., 6cm) ?, and can indirectly scale the box to the textwidth as follows (note I add a lipsum paragraph to show it is the proper width). To do this, I save the figure in \x. Then I find the natural width of \x, so that I can develop the proper scale ratio. Then I \scalebox the image to the proper scale:

\documentclass{report}
\usepackage{chemfig}
\usepackage[version=3]{mhchem}
\usepackage{lipsum}
\makeatletter
\newcommand*{\DivideLengths}[2]{%
    \strip@pt\dimexpr\number\numexpr\number\dimexpr#1%
     \relax*65536/\number\dimexpr#2\relax\relax sp\relax
}
\makeatother
\global\newlength\thewidth
\newcommand\findwidth[1]{\setbox0\hbox{#1}\setlength\thewidth{\wd0}}
\begin{document}
\def\x{
\schemestart
\ce{O2^.-}\arrow (c1--c2){0}\ce{NO^.}\merge{v}(c1)(c2)--(c3)\ce{ONOO-}
\arrow (--c4){<=>[*{0}\parbox{1.2cm}{\ce{+ H+}\\ pK 6.6}][*{0}\ce{- H+}]}[-90,1.5]\ce{ONOOH}
\arrow(--c5){->[\ce{+ NO^.}][\ce{- HNO2}]}[0,1.5]\ce{NO2^.}
\arrow(--c6){->[\ce{+ NO^.}]}\ce{N2O3}
\arrow(@c4--c7){->[\ce{+ O2^.-}][\ce{- OH-},\ce{- O2}]}[180,1.5]\ce{NO2^.}
\arrow(@c7--c8){->[\ce{+ O2^.-}]}[180]\parbox{1.5cm}{\centering \ce{O2NOO-}\\or \\\ce{NO2- + O2}}
\arrow(@c3--@c7){->[\ce{+ CO2}][\ce{- CO3^.}]}[2.12]
\arrow(@c3--@c5){->[\ce{+ CO2}][\ce{- CO3^.-}]}[2.12]
\arrow(@c1--c9){->[\ce{+ O2^.-}, \ce{+ 2H+}][\ce{- O2}]}[180,1.5]\ce{H2O2}
\arrow(@c9--c10){->[\ce{Fe2+}][\ce{- OH-}]}[180]\ce{^.OH}
\arrow(@c2--c11){->[\ce{+ O2}]}$[$\ce{^.ONOO}$]$
\arrow(@c11--c12){->[\ce{+ ^.NO}]}\ce{2 NO2^.}
\schemestop
}
\findwidth{\x}
\begin{figure}[width=.9\textwidth]
\scalebox{\DivideLengths{\textwidth}{\thewidth}}{\x}
\end{figure}
\lipsum[1]
\end{document}

enter image description here

Related Question