pandoc-latex-template icon indicating copy to clipboard operation
pandoc-latex-template copied to clipboard

How do I center images?

Open m4ttsch opened this issue 4 years ago • 10 comments

Currently, my images stretch horizontally to fill the page. This is too large, so I rescaled the images by 50%. Unfortunately, when they get burned into the PDF, they are left-aligned. What can I edit in the template to center-align images? I am quite new to Latex.

m4ttsch avatar May 09 '20 17:05 m4ttsch

@m4ttsch

\begin{center} %your image or figure here \end{center}

Cy-dev-tex avatar May 12 '20 18:05 Cy-dev-tex

Yes thank you, a quick google search answered that question for me. Where should I put that in this file? https://github.com/Wandmalfarbe/pandoc-latex-template/blob/master/eisvogel.tex

m4ttsch avatar May 12 '20 18:05 m4ttsch

@m4ttsch I'll let you know when I submit my pull-request

Cy-dev-tex avatar May 12 '20 19:05 Cy-dev-tex

@m4ttsch the file you referenced is very busy. Your stating commands which are typically left in the preamble very far into the document. You might do better to make a separate sty and/or cls file(s) to make your code more manageable. Your reference to figures seems limited to whether or not your employing the float [H] to place the figure, but nothing more. perhaps an environment could be used to handle your float and centering of figures. The code strikes me as unorthodox, so I'd probably make major changes to your project that you may not feel comfortable with.

Cy-dev-tex avatar May 12 '20 20:05 Cy-dev-tex

This ... is not my project. I didn't write this template. I'm using it to format some of my own markdown and I was hoping that someone (the author, most likely) could point me in a direction that could help me center images in my outputted PDF file.

m4ttsch avatar May 12 '20 20:05 m4ttsch

@m4ttsch Please share your repository or code so I can be of assistance.

Cy-dev-tex avatar May 12 '20 20:05 Cy-dev-tex

you could try https://tex.stackexchange.com/questions/262380/how-to-automatically-center-images, i.e. you would need to redefine the \includegraphics command which is generated by the latex writer in pandoc.

you could put the lines in a center.tex file and add it using -H center.tex ...

cagix avatar May 13 '20 07:05 cagix

you also could add a class to your markdown image like ![](myimage.png){.center} and add the proposed \begin{center} and \end{center} for all images with class center using a simple lua filter ...

cagix avatar May 13 '20 07:05 cagix

This solution works for me:

https://stackoverflow.com/questions/24677642/centering-image-and-text-in-r-markdown-for-a-pdf-report

The simple solution given by Jonathan works with a modification to cheat Pandoc. Instead of direct Latex commands such as

\begin{center}
Text
\end{center}

you can define your own commands in the YAML header:

header-includes:
- \newcommand{\bcenter}{\begin{center}}
- \newcommand{\ecenter}{\end{center}}

And then you use:

\bcenter
Text and more
\ecenter

This works for me for centering a whole document with many code chunks and markdown commands in between.

krahets avatar Feb 16 '23 20:02 krahets

It's also possible to center all images with lua filter script.

-- center-images.lua

function Image (elem)
    -- Surround all images with image-centering raw LaTeX.
    return {
      pandoc.RawInline('latex', '\\hfill\\break{\\centering'),
      elem,
      pandoc.RawInline('latex', '\\par}')
    }
end
pandoc src/*.md -o paper.pdf --lua-filter=center-images.lua

GregoryKogan avatar Mar 10 '24 16:03 GregoryKogan