{"id":17,"date":"2022-07-01T12:00:00","date_gmt":"2022-07-01T12:00:00","guid":{"rendered":"https:\/\/blog.marcinpolkowski.com\/?p=17"},"modified":"2022-10-05T22:29:42","modified_gmt":"2022-10-05T22:29:42","slug":"generating-images-with-latex-equations","status":"publish","type":"post","link":"https:\/\/blog.marcinpolkowski.com\/?p=17","title":{"rendered":"Generating images with LaTeX equations"},"content":{"rendered":"<p>Sometimes it is useful to generate an image (like a PNG file) with a high-resolution mathematical equation. I know that there are online tools for doing that but imagine you are like me and prefer getting things done &#8220;in-house&#8221;.<\/p>\n<p>I will assume you have docker available (if not, go to https:\/\/www.docker.com\/ and install Docker Desktop).<\/p>\n<p>In the first step we will need to get two images from Docker Hub (we do that in terminal \/ command line):<br \/>\n<center><\/p>\n<pre style=\"width:90%; overflow:auto; text-align:left;\"><code class=\"language-bash\">docker pull miktex\/miktex\ndocker pull dpokidov\/imagemagick\n<\/code><\/pre>\n<p><\/center><\/p>\n<p>Both commands will download some stuff and should be ready in a minute or so. Now we will need our LaTeX code that defines the equation:<br \/>\n<center><\/p>\n<pre style=\"width:90%; overflow:auto; text-align:left;\"><code class=\"language-bash\">\\documentclass{article}\n\\usepackage{amsmath}\n\\pagenumbering{gobble}\n\\begin{document}\n\\begin{equation*}\ne^{i\\pi }+1=0\n\\end{equation*}\n\\end{document}\n<\/code><\/pre>\n<p><\/center><\/p>\n<p>Now assuming that the LaTeX code is saved in <code class=\"language-bash\">test.tex<\/code> file we will need to execute 3 steps:<br \/>\n<center><\/p>\n<pre style=\"width:90%; overflow:auto; text-align:left;\"><code class=\"language-bash\">docker run --rm -v miktex:\/miktex\/.miktex -v $PWD:\/miktex\/work miktex\/miktex latex test.tex\ndocker run --rm -v miktex:\/miktex\/.miktex -v $PWD:\/miktex\/work miktex\/miktex dvipng -D 600 -bg Transparent test.dvi\ndocker run --rm -v $PWD:\/imgs dpokidov\/imagemagick imgs\/test1.png -trim imgs\/test2.png\n<\/code><\/pre>\n<p><\/center><\/p>\n<p>The first command uses <code class=\"language-bash\">miktex\/miktex<\/code> Docker Image to &#8220;compile&#8221; out LaTeX file to DVI format.<\/p>\n<p>The second command uses the same Docker Image to convert DVI into PNG image. Our LaTeX document has one page, so one PNG will be created after conversion (<code class=\"language-bash\">test1.png<\/code>). Unfortunately, we are not done yet, because our PNG image has strange margins.<\/p>\n<p>In the last step, we fix those margins by using <code class=\"language-bash\">dpokidov\/imagemagick<\/code> Docker Image. Our final result looks like this (<code class=\"language-bash\">test2.png<\/code>):<br \/>\n<center><img decoding=\"async\" src=\"https:\/\/blog.marcinpolkowski.com\/wp-content\/uploads\/2022\/05\/test2.png\"><\/center><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Sometimes it is useful to generate an image (like a PNG file) with a high-resolution mathematical equation. I know that there are online tools for doing that but imagine you are like me and prefer getting things done &#8220;in-house&#8221;. I will assume you have docker available (if not, go to https:\/\/www.docker.com\/ and install Docker Desktop). [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[3,4],"tags":[],"class_list":["post-17","post","type-post","status-publish","format-standard","hentry","category-docker","category-latex"],"_links":{"self":[{"href":"https:\/\/blog.marcinpolkowski.com\/index.php?rest_route=\/wp\/v2\/posts\/17","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/blog.marcinpolkowski.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blog.marcinpolkowski.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blog.marcinpolkowski.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/blog.marcinpolkowski.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=17"}],"version-history":[{"count":9,"href":"https:\/\/blog.marcinpolkowski.com\/index.php?rest_route=\/wp\/v2\/posts\/17\/revisions"}],"predecessor-version":[{"id":28,"href":"https:\/\/blog.marcinpolkowski.com\/index.php?rest_route=\/wp\/v2\/posts\/17\/revisions\/28"}],"wp:attachment":[{"href":"https:\/\/blog.marcinpolkowski.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=17"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blog.marcinpolkowski.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=17"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blog.marcinpolkowski.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=17"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}