Wandmalfarbe / pandoc-latex-template

A pandoc LaTeX template to convert markdown files to PDF or LaTeX.
BSD 3-Clause "New" or "Revised" License
6.21k stars 968 forks source link

How do I center images? #173

Open m4ttsch opened 4 years ago

m4ttsch commented 4 years ago

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.

Cy-dev-tex commented 4 years ago

@m4ttsch

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

m4ttsch commented 4 years ago

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

Cy-dev-tex commented 4 years ago

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

Cy-dev-tex commented 4 years ago

@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.

m4ttsch commented 4 years ago

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.

Cy-dev-tex commented 4 years ago

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

cagix commented 4 years ago

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 commented 4 years ago

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 ...

krahets commented 1 year ago

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.

GregoryKogan commented 7 months ago

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