Skip to content

Commit 0682c60

Browse files
Latex: images without width displayed at natural size if possible
1 parent e79d582 commit 0682c60

File tree

1 file changed

+29
-2
lines changed

1 file changed

+29
-2
lines changed

xsl/pretext-latex-common.xsl

Lines changed: 29 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -405,6 +405,7 @@ along with MathBook XML. If not, see <http://www.gnu.org/licenses/>.
405405
<xsl:call-template name="chapter-start-number"/>
406406
<xsl:call-template name="equation-numbering"/>
407407
<xsl:call-template name="image-tcolorbox"/>
408+
<xsl:call-template name="image-sizing"/>
408409
<xsl:call-template name="tables"/>
409410
<xsl:call-template name="footnote-numbering"/>
410411
<xsl:call-template name="font-awesome"/>
@@ -1380,14 +1381,32 @@ along with MathBook XML. If not, see <http://www.gnu.org/licenses/>.
13801381
</xsl:if>
13811382
</xsl:template>
13821383

1384+
<!-- image sizing support -->
1385+
<xsl:template name="image-sizing">
1386+
<xsl:if test="$document-root//image">
1387+
<xsl:text>%% Define maxwidth variable for includegraphics&#xa;</xsl:text>
1388+
<xsl:text>%% recipe from https://texfaq.org/FAQ-grmaxwidth &#xa;</xsl:text>
1389+
<xsl:text>\makeatletter&#xa;</xsl:text>
1390+
1391+
<xsl:text>\def\maxwidth{%&#xa;</xsl:text>
1392+
<xsl:text> \ifdim\Gin@nat@width&gt;\linewidth&#xa;</xsl:text>
1393+
<xsl:text> \linewidth&#xa;</xsl:text>
1394+
<xsl:text> \else&#xa;</xsl:text>
1395+
<xsl:text> \Gin@nat@width&#xa;</xsl:text>
1396+
<xsl:text> \fi&#xa;</xsl:text>
1397+
<xsl:text>}&#xa;</xsl:text>
1398+
<xsl:text>\makeatother&#xa;</xsl:text>
1399+
</xsl:if>
1400+
</xsl:template>
1401+
13831402
<!-- tcolorbox for images -->
13841403
<xsl:template name="image-tcolorbox">
13851404
<xsl:if test="$document-root//image">
13861405
<xsl:text>%% "tcolorbox" environment for a single image, occupying entire \linewidth&#xa;</xsl:text>
13871406
<xsl:text>%% arguments are left-margin, width, right-margin, as multiples of&#xa;</xsl:text>
13881407
<xsl:text>%% \linewidth, and are guaranteed to be positive and sum to 1.0&#xa;</xsl:text>
13891408
<xsl:text>\tcbset{ imagestyle/.style={bwminimalstyle} }&#xa;</xsl:text>
1390-
<xsl:text>\NewTColorBox{tcbimage}{mmm}{imagestyle,left skip=#1\linewidth,width=#2\linewidth}&#xa;</xsl:text>
1409+
<xsl:text>\NewTColorBox{tcbimage}{mmm}{imagestyle,left skip=#1\linewidth,width=#2\linewidth,halign=center}&#xa;</xsl:text>
13911410
<xsl:text>%% Wrapper environment for tcbimage environment with a fourth argument&#xa;</xsl:text>
13921411
<xsl:text>%% Fourth argument, if nonempty, is a vertical space adjustment&#xa;</xsl:text>
13931412
<xsl:text>%% and implies image will be preceded by \leavevmode\nopagebreak&#xa;</xsl:text>
@@ -9277,7 +9296,15 @@ along with MathBook XML. If not, see <http://www.gnu.org/licenses/>.
92779296
</xsl:with-param>
92789297
</xsl:call-template>
92799298
</xsl:variable>
9280-
<xsl:text>\includegraphics[width=\linewidth</xsl:text>
9299+
<xsl:text>\includegraphics[width=</xsl:text>
9300+
<xsl:choose>
9301+
<xsl:when test="@width">
9302+
<xsl:text>\linewidth</xsl:text>
9303+
</xsl:when>
9304+
<xsl:otherwise>
9305+
<xsl:text>\maxwidth</xsl:text>
9306+
</xsl:otherwise>
9307+
</xsl:choose>
92819308
<xsl:if test="@rotate">
92829309
<xsl:text>,angle=</xsl:text>
92839310
<xsl:value-of select="@rotate"/>

0 commit comments

Comments
 (0)