Centering a box independent of documents margin

Is there a way to center a box (for example, adjustbox) even if the documents margins are not equal (left and right)?

\documentclass{article}
\usepackage[pass]{geometry}
\usepackage{lipsum}

\begin{document}

\lipsum[2]

\newgeometry{left=250pt,top=1cm,bottom=1cm,includefoot}
\lipsum[2]

\end{document}


You can put it inside a tikz node with position (current page.center) as follows:

\documentclass{article}
\usepackage[pass]{geometry}
\usepackage{lipsum}
\usepackage{tikz}

\begin{document}

\begin{tikzpicture}[overlay, remember picture]
\node[text width=0.7*\linewidth] at (current page.center)
{\lipsum[2]};
\end{tikzpicture}

\newgeometry{left=250pt,top=1cm,bottom=1cm,includefoot}

\begin{tikzpicture}[overlay, remember picture]
\node[text width=0.7*\linewidth] at (current page.center)
{\lipsum[2]};
\end{tikzpicture}

\end{document}


With the newest version of adjustbox v1.2 from 2019/01/04 you can use the pagecenter key to align the content centered on the page. This uses the code in my original answer internally (i.e. lapping the content to the left depending on the page margin).

\documentclass[twopages]{book}
\usepackage{blindtext}

\begin{document}
\blindtext

\blindtext

\blindtext

\clearpage
\blindtext

\blindtext

\blindtext

\end{document}


Original answer which works with older versions of adjustbox:

If you want to center the box on the page, not the text area, one way to do this is to make it lap into the left text margin of the page. The required dimensions can be read using the layout package.

The following code uses the ifoddpage package together with adjustbox to get the right amount.

\documentclass[twopages]{book}
\usepackage{ifoddpage}
\usepackage[pass,showframe]{geometry}
\usepackage{layout}
\usepackage{lipsum}

\begin{document}
\lipsum[1]

\checkoddpage
lap={\textwidth}{-1in-\hoffset-\ifoddpage\oddsidemargin\else\evensidemargin\fi+.5\paperwidth-.5\width-\csname @totalleftmargin\endcsname}}
\lipsum[2]

\lipsum

\checkoddpage
lap={\textwidth}{-1in-\hoffset-\ifoddpage\oddsidemargin\else\evensidemargin\fi+.5\paperwidth-.5\width-\csname @totalleftmargin\endcsname}}
\lipsum[2]

\lipsum[1]

\clearpage
\layout

\end{document}


• As I liked the idea I now added this as centerpage key as well as \centerpagebox macro and centerpagebox to the adjustbox package. It will be published with the next release. – Martin Scharrer Dec 28 '18 at 11:42

It is not clear exactly what you are centering on, but you can use the adjustwith command from the changepage package to temporarily adjust margins.

The command takes two arguments for left and right margin respectively. The value is the amount by which the text block will be REDUCED (so negative values enlarge). The effect is on linewidth not textwidth.

The below MWE and output together with some reading should explain.

\documentclass{article}
\usepackage[pass]{geometry}
\usepackage{changepage}

\begin{document}

\newgeometry{left=100pt,top=1cm,bottom=1cm,includefoot}

\begin{center}
Center within margins
\end{center}
Left
\flushright Right

\newgeometry{left=200pt,top=1cm,bottom=1cm,includefoot}

\begin{center}
Center within margins
\end{center}
\begin{center}
\end{center}
Left
\flushright Right

\newgeometry{left=300pt,top=1cm,bottom=1cm,includefoot}

\begin{center}
Center within margins
\end{center}
\begin{center}
\end{center}
Text
\flushright Right

\newgeometry{left=400pt,top=1cm,bottom=1cm,includefoot}

\begin{center}
Center within margins
\end{center}