LyXCentering image on page

Information and discussion about LyX, a WYSIWYM editor, available for Linux, Windows and Mac OS X systems.
johnny7
Posts: 21
Joined: Fri Oct 24, 2014 12:25 pm

Centering image on page

Post by johnny7 »

Hi,

In the appendix, I want to include several images, one image per page (imagine reproductions of a manuscript). Centering it horizontally works fine, but how can I vertically center the image on the page? I was unable to find an answer online, there must be a simple solution.

Thanks!

Edit: The question has indeed been asked before, but no answer was provided: http://latex.org/forum/viewtopic.php?f=45&t=16016

Edit2: Okay, placing \vspace*{\fill} before and after the image should do the deal! Sometimes one has to ask first before being able to find a solution for himself.

Recommended reading 2024:

LaTeXguide.org • LaTeX-Cookbook.net • TikZ.org

NEW: TikZ book now 40% off at Amazon.com for a short time.

And: Currently, Packt sells ebooks for $4.99 each if you buy 5 of their over 1000 ebooks. If you choose only a single one, $9.99. How about combining 3 LaTeX books with Python, gnuplot, mathplotlib, Matlab, ChatGPT or other AI books? Epub and PDF. Bundle (3 books, add more for higher discount): https://packt.link/MDH5p

Stefan Kottwitz
Site Admin
Posts: 10335
Joined: Mon Mar 10, 2008 9:44 pm

Centering image on page

Post by Stefan Kottwitz »

Hi johnny,

no problem! Great to see that your question is quickly solved, an thanks for posting your solution.

By the way, for centering on the page the margins should be the same for top and bottom. The command aboves centers within the text area.

Stefan
LaTeX.org admin
johnny7
Posts: 21
Joined: Fri Oct 24, 2014 12:25 pm

Centering image on page

Post by johnny7 »

Okay, maybe I was too quick to consider it solved. I was perfectly able to center one image on a page. However, oddly, several images added behind each other do not align. I tried it with and without a new page or a page break between each image. They are all identical with \vspace*{\fill} before and after the image. Why are they nevertheless positioned differently?

I am working with LyX but the code looks like this:

\newpage{}
\begin{center}
\vspace*{\fill}\includegraphics[width=15cm]{\string"image1".png}\vspace*{\fill}
\par\end{center}
User avatar
Stefan Kottwitz
Site Admin
Posts: 10335
Joined: Mon Mar 10, 2008 9:44 pm

Centering image on page

Post by Stefan Kottwitz »

The \vspace*{\fill} commands should be outside the center environment, so before \begin{center} and after \end{center}.

Stefan
LaTeX.org admin
johnny7
Posts: 21
Joined: Fri Oct 24, 2014 12:25 pm

Centering image on page

Post by johnny7 »

Thanks! This didn't change anything though. I also noticed that it makes a difference if a comment section is added at the end or not (why?). Now all images are a bit too low on the page, that is the gap to the bottom is smaller, although the margins are set the same.
User avatar
Stefan Kottwitz
Site Admin
Posts: 10335
Joined: Mon Mar 10, 2008 9:44 pm

Centering image on page

Post by Stefan Kottwitz »

Interesting! But there should be a simple explanation. Could you post a complete example? Either constructing it based on your document, or a reduced copy of your document without the actual text.

Stefan
LaTeX.org admin
johnny7
Posts: 21
Joined: Fri Oct 24, 2014 12:25 pm

Centering image on page

Post by johnny7 »

Can you work with a lyx file? In this case I'll upload one.
User avatar
Stefan Kottwitz
Site Admin
Posts: 10335
Joined: Mon Mar 10, 2008 9:44 pm

Centering image on page

Post by Stefan Kottwitz »

I don't have LyX on my notebook and I'm traveling. However, a pdfLaTeX export of the LyX file would be perfect too. It should behave exactly the same, and shows the LaTeX code generated by LyX.

Stefan
LaTeX.org admin
johnny7
Posts: 21
Joined: Fri Oct 24, 2014 12:25 pm

Centering image on page

Post by johnny7 »

Okay, now, after I deleted the closing comment section, the last image is aligned with the first. They seem all placed a bit too low, is that right?
I've got another question for which I'll open another thread.
Edit: Well, the other question concerned hyphenation. My hyphenation rule did not work because \usepackage{babel} followed after \hyphenation{Renais-sance}. To put it behind I seem to have to include it as a TeX code in the LyX document itself instead of the preamble.

Code: Select all

\documentclass[12pt,ngerman]{article}
\usepackage{mathptmx}
\usepackage[T1]{fontenc}
\usepackage[latin9]{inputenc}
\usepackage[a4paper]{geometry}
\geometry{verbose,tmargin=2.5cm,bmargin=2.5cm,lmargin=2.5cm,rmargin=2.5cm}
\usepackage{graphicx}
\usepackage{setspace}
\makeatletter
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% User specified LaTeX commands.
\usepackage{blindtext}
\hyphenation{Renais-sance}
\makeatother
\usepackage{babel}
\begin{document}
\appendix
\section{Anhang}
\subsection{Test}
\blindtext
\subsection{Hyphenation}
Renaissance Renaissance Renaissance Renaissance Renaissance Renaissance
Renaissance Renaissance Renaissance Renaissance Renaissance Renaissance
Renaissance Renaissance Renaissance Renaissance
\newpage{}

\vspace*{\fill}
\begin{center}
\includegraphics[width=15cm]{test}
\par\end{center}

\vspace*{\fill}

\newpage{}

\vspace*{\fill}
\begin{center}
\includegraphics[width=15cm]{test}
\par\end{center}

\vspace*{\fill}

\newpage{}

\vspace*{\fill}
\begin{center}
\includegraphics[width=15cm]{test}
\par\end{center}

\vspace*{\fill}
\end{document}
User avatar
Stefan Kottwitz
Site Admin
Posts: 10335
Joined: Mon Mar 10, 2008 9:44 pm

Centering image on page

Post by Stefan Kottwitz »

Good that you found it! I'm off for 10 hours now, flying to Miami. :)

Stefan
LaTeX.org admin
Post Reply