{"id":948,"date":"2016-02-17T08:36:17","date_gmt":"2016-02-17T08:36:17","guid":{"rendered":"https:\/\/block.arch.ethz.ch\/blog\/?p=948"},"modified":"2016-07-07T07:56:39","modified_gmt":"2016-07-07T07:56:39","slug":"latex-fill-image-proportionally","status":"publish","type":"post","link":"https:\/\/block.arch.ethz.ch\/blog\/2016\/02\/latex-fill-image-proportionally\/","title":{"rendered":"LaTeX fill image proportionally"},"content":{"rendered":"<p>At present it is quite easy in LaTeX to fit an image proportionally to a frame of fixed width and height. However, to fill a frame with that same image is not straightforward&#8230;<\/p>\n<p><!--more--><\/p>\n<p>This is a snippet that fits the image proportionally to the frame:<\/p>\n<pre class=\"brush:latex;toolbar:false;gutter:false\">\r\n\\begin{figure}[ht]\r\n  \\includegraphics[width=60mm,height=40mm,keepaspectratio]{image}\r\n  \\caption{Kakamigahara Crematorium, Japan}\r\n\\end{figure}\r\n<\/pre>\n<p>To fill the frame, we first have to import two packages at the start of your document.<\/p>\n<pre class=\"brush:latex;toolbar:false;gutter:false\">\r\n\\usepackage[export]{adjustbox}\r\n\\usepackage[nomessages]{fp}\r\n<\/pre>\n<p>followed by this command (developed from <a href=\"http:\/\/tex.stackexchange.com\/questions\/25988\/scale-included-graphics-to-the-higher-ratio-instead-of-the-lower-for-given-width\">this post<\/a> on StackExchange);<\/p>\n<pre class=\"brush:latex;toolbar:false;gutter:false\">\r\n\\newcommand{\\includegraphicx}[5]{\r\n\\FPeval{\\mywidtha}{     #3*2.845274*#1}\r\n\\FPeval{\\mywidthb}{ (1-#3)*2.845274*#1}\r\n\\FPeval{\\myheighta}{    #4*2.845274*#2}\r\n\\FPeval{\\myheightb}{(1-#4)*2.845274*#2}%\r\n\\adjustbox{min size={#1mm}{#2mm},\r\n    Clip*={#3\\width - \\mywidtha}   {#4\\height - \\myheighta}\r\n          {#3\\width + \\mywidthb}  {#4\\height + \\myheightb}}%           \r\n          {\\includegraphics[max size={#1mm}{#2mm}]{#5}}}\r\n\r\n\\begin{document}\r\n<\/pre>\n<p>The factor 2.84574 converts from mm to pt.<br \/>\nThen, we can use the command to fill a frame. In this case, 60mm width, 40mm height, positioned at 50% of the horizontal and vertical dimensions of the original image.<\/p>\n<pre class=\"brush:latex;toolbar:false;gutter:false\">\r\n\\begin{figure}[ht]\r\n  \\includegraphicx{60}{40}{0.5}{0.5}{image}\r\n  \\caption{Kakamigahara Crematorium, Japan}\r\n\\end{figure}\r\n<\/pre>\n<p>This definition works, but could be further improved to allow any unit for the width and height.<br \/>\nThe resulting two types of figures, fitted and filled, will look like this:<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/block.arch.ethz.ch\/blog\/wp-content\/uploads\/2016\/02\/latex-kakamigahara-200x300.png\" alt=\"latex-kakamigahara\" width=\"200\" height=\"300\" class=\"alignnone size-medium wp-image-950\" srcset=\"https:\/\/block.arch.ethz.ch\/blog\/wp-content\/uploads\/2016\/02\/latex-kakamigahara-200x300.png 200w, https:\/\/block.arch.ethz.ch\/blog\/wp-content\/uploads\/2016\/02\/latex-kakamigahara.png 309w\" sizes=\"auto, (max-width: 200px) 100vw, 200px\" \/><\/p>\n","protected":false},"excerpt":{"rendered":"<p>At present it is quite easy in LaTeX to fit an image proportionally to a frame of fixed width and height. However, to fill a frame with that same image is not straightforward&#8230;<\/p>\n","protected":false},"author":13,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-948","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"_links":{"self":[{"href":"https:\/\/block.arch.ethz.ch\/blog\/wp-json\/wp\/v2\/posts\/948","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/block.arch.ethz.ch\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/block.arch.ethz.ch\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/block.arch.ethz.ch\/blog\/wp-json\/wp\/v2\/users\/13"}],"replies":[{"embeddable":true,"href":"https:\/\/block.arch.ethz.ch\/blog\/wp-json\/wp\/v2\/comments?post=948"}],"version-history":[{"count":12,"href":"https:\/\/block.arch.ethz.ch\/blog\/wp-json\/wp\/v2\/posts\/948\/revisions"}],"predecessor-version":[{"id":1026,"href":"https:\/\/block.arch.ethz.ch\/blog\/wp-json\/wp\/v2\/posts\/948\/revisions\/1026"}],"wp:attachment":[{"href":"https:\/\/block.arch.ethz.ch\/blog\/wp-json\/wp\/v2\/media?parent=948"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/block.arch.ethz.ch\/blog\/wp-json\/wp\/v2\/categories?post=948"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/block.arch.ethz.ch\/blog\/wp-json\/wp\/v2\/tags?post=948"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}