# How to center pdf image?

I'm using pdftex, and I'm trying to center an image with the rest of my text.

I want to get a result like:

{\hfil \pdfximage{test.png} \pdfrefximage\pdflastximage \hfil}
\bye


gives, but instead I'm trying to use this format so that I don't have to use a bunch of clunky groups and hfils everywhere:

\everypar={\leftskip=0pt plus 1fil \rightskip=\leftskip \parindent=0pt \parfillskip=0pt}
\hbox{}
\par
Here's some of my text

and a second line, but images don't work:

\pdfximage height 3in {test.png}\relax\pdfrefximage\pdflastximage
\bye


How can I get the images to center? I've tried \immediate and moving the declaration out of \everypar, but neither have worked.

For reference, results of first and second code's results in order:

• Do you want the rest of your text to be centered as well (because the \everypar stuff is causing that). Otherwise you can just define a new macro for centered images, such that you don't have to repeat the commands all the time. – siracusa Aug 7 '19 at 23:50
• @siracusa That's the reason I'm using \everypar in the first place. I realise that I could make a special case for images, but I'm wondering if it can be fixed "generally." – Holden Rohrer Aug 8 '19 at 0:16

I don't understand the need of the everypar, you can set \leftskip and \rightright (see below, transported from latex code). \pdfrefximage doesn't quit vmode, so you need a \noindent before it.

\rightskip0pt plus 1fil
\leftskip0pt plus 1fil
\parindent0pt
\parfillskip0pt plus0pt minus0pt

some text

\par

some text

\pdfximage height 1in {example-image.png}\relax\pdfrefximage\pdflastximage

\noindent\pdfximage height 1in {example-image.png}\relax\pdfrefximage\pdflastximage

\bye


• This worked for me. Thanks. And you're right about not needing \everypar. I think it might have been a hold-over from some debugging. – Holden Rohrer Aug 8 '19 at 6:33