Skip to content

Commit e692eac

Browse files
Latex: images without width displayed at natural size if possible
1 parent 1249ccc commit e692eac

1 file changed

Lines changed: 29 additions & 2 deletions

File tree

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"/>
@@ -1384,14 +1385,32 @@ along with MathBook XML. If not, see <http://www.gnu.org/licenses/>.
13841385
</xsl:if>
13851386
</xsl:template>
13861387

1388+
<!-- image sizing support -->
1389+
<xsl:template name="image-sizing">
1390+
<xsl:if test="$document-root//image">
1391+
<xsl:text>%% Define maxwidth variable for includegraphics&#xa;</xsl:text>
1392+
<xsl:text>%% recipe from https://texfaq.org/FAQ-grmaxwidth &#xa;</xsl:text>
1393+
<xsl:text>\makeatletter&#xa;</xsl:text>
1394+
1395+
<xsl:text>\def\maxwidth{%&#xa;</xsl:text>
1396+
<xsl:text> \ifdim\Gin@nat@width&gt;\linewidth&#xa;</xsl:text>
1397+
<xsl:text> \linewidth&#xa;</xsl:text>
1398+
<xsl:text> \else&#xa;</xsl:text>
1399+
<xsl:text> \Gin@nat@width&#xa;</xsl:text>
1400+
<xsl:text> \fi&#xa;</xsl:text>
1401+
<xsl:text>}&#xa;</xsl:text>
1402+
<xsl:text>\makeatother&#xa;</xsl:text>
1403+
</xsl:if>
1404+
</xsl:template>
1405+
13871406
<!-- tcolorbox for images -->
13881407
<xsl:template name="image-tcolorbox">
13891408
<xsl:if test="$document-root//image">
13901409
<xsl:text>%% "tcolorbox" environment for a single image, occupying entire \linewidth&#xa;</xsl:text>
13911410
<xsl:text>%% arguments are left-margin, width, right-margin, as multiples of&#xa;</xsl:text>
13921411
<xsl:text>%% \linewidth, and are guaranteed to be positive and sum to 1.0&#xa;</xsl:text>
13931412
<xsl:text>\tcbset{ imagestyle/.style={bwminimalstyle} }&#xa;</xsl:text>
1394-
<xsl:text>\NewTColorBox{tcbimage}{mmm}{imagestyle,left skip=#1\linewidth,width=#2\linewidth}&#xa;</xsl:text>
1413+
<xsl:text>\NewTColorBox{tcbimage}{mmm}{imagestyle,left skip=#1\linewidth,width=#2\linewidth,halign=center}&#xa;</xsl:text>
13951414
<xsl:text>%% Wrapper environment for tcbimage environment with a fourth argument&#xa;</xsl:text>
13961415
<xsl:text>%% Fourth argument, if nonempty, is a vertical space adjustment&#xa;</xsl:text>
13971416
<xsl:text>%% and implies image will be preceded by \leavevmode\nopagebreak&#xa;</xsl:text>
@@ -9294,7 +9313,15 @@ along with MathBook XML. If not, see <http://www.gnu.org/licenses/>.
92949313
</xsl:with-param>
92959314
</xsl:call-template>
92969315
</xsl:variable>
9297-
<xsl:text>\includegraphics[width=\linewidth</xsl:text>
9316+
<xsl:text>\includegraphics[width=</xsl:text>
9317+
<xsl:choose>
9318+
<xsl:when test="@width">
9319+
<xsl:text>\linewidth</xsl:text>
9320+
</xsl:when>
9321+
<xsl:otherwise>
9322+
<xsl:text>\maxwidth</xsl:text>
9323+
</xsl:otherwise>
9324+
</xsl:choose>
92989325
<xsl:if test="@rotate">
92999326
<xsl:text>,angle=</xsl:text>
93009327
<xsl:value-of select="@rotate"/>

0 commit comments

Comments
 (0)