2

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{adjustbox}
\usepackage[pass]{geometry}
\usepackage{lipsum}

\begin{document}

\begin{adjustbox}{minipage=0.99\paperwidth,margin=0pt,center}
\lipsum[2]
\end{adjustbox}

\newgeometry{left=250pt,top=1cm,bottom=1cm,includefoot}
\begin{adjustbox}{minipage=0.99\paperwidth,margin=0pt,center}
\lipsum[2]
\end{adjustbox}

\end{document}
2

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

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


\begin{document}



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


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


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


\end{document}
1

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{adjustbox}[2019/01/04]
\usepackage{blindtext}

\begin{document}
\blindtext

\begin{adjustbox}{minipage=0.95\paperwidth,frame,pagecenter,vspace=\medskipamount}
\blindtext
\end{adjustbox}

\blindtext

\clearpage
\blindtext

\begin{adjustbox}{minipage=0.95\paperwidth,frame,pagecenter,vspace=\medskipamount}
\blindtext
\end{adjustbox}

\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{adjustbox}
\usepackage[pass,showframe]{geometry}
\usepackage{layout}
\usepackage{lipsum}


\begin{document}
\lipsum[1]

\checkoddpage
\begin{adjustbox}{minipage=0.95\paperwidth,vspace=\smallskipamount,frame,
lap={\textwidth}{-1in-\hoffset-\ifoddpage\oddsidemargin\else\evensidemargin\fi+.5\paperwidth-.5\width-\csname @totalleftmargin\endcsname}}
\lipsum[2]
\end{adjustbox}

\lipsum

\checkoddpage
\begin{adjustbox}{minipage=0.95\paperwidth,vspace=\smallskipamount,frame,
lap={\textwidth}{-1in-\hoffset-\ifoddpage\oddsidemargin\else\evensidemargin\fi+.5\paperwidth-.5\width-\csname @totalleftmargin\endcsname}}
\lipsum[2]
\end{adjustbox}

\lipsum[1]

\clearpage
\layout


\end{document}

enter image description here

enter image description here

  • 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
0

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{adjustwidth}{-100pt}{0pt}
    \begin{center}
        Center on temporarily adjusted margin
    \end{center}    
Left
\flushright Right
\end{adjustwidth}

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

\begin{center}
    Center within margins
\end{center}    
\begin{adjustwidth}{-200pt}{0pt}
    \begin{center}
        Center on temporarily adjusted margin
    \end{center}    
Text
\flushright Right
\end{adjustwidth}

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

\begin{center}
    Center within margins
\end{center}    
\begin{adjustwidth}{-300pt}{0pt}
    \begin{center}
        Center on temporarily adjusted margin
    \end{center}    
Text
\flushright Right
\end{adjustwidth}

\end{document}

enter image description here

It is possible to automate slightly but depends what you are centering on. Manual use may be easiest when you understand how it works. Obviously if you have different margins on odd and even pages that needs taking into account as well.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.