[Tex/LaTex] Moving minipage on page to flush right with text

minipagepositioningwatermarkxwatermark

I would like to place a box on every page of my LaTeX document that does not interfere with the typesetting of the rest of the document, and found that the watermark packages are suitable for the task; e.g., xwatermark.

Specifically, I would like to insert a framed minipage such that its right border coincides with the right border of the text body.

Considering the LaTeX page layout, I tried

\documentclass{article}

\usepackage{lipsum}

\newlength\xshift
\setlength\xshift{0.5\paperwidth}
\addtolength\xshift{-\marginparwidth}
\addtolength\xshift{-\marginparsep}
\addtolength\xshift{-1.0in} % half the minipage width
\addtolength\xshift{-1pt} % frame width

\usepackage{xcolor}
\usepackage[printwatermark]{xwatermark}
\newwatermark[
pages=1,
color=black,
scale=1,
xpos=\xshift,
ypos=0.0in
]{%
  \fbox{%
  \begin{minipage}{2.0in}\normalsize%
    my awesome minipage\\
    my awesome minipage\\
    my awesome minipage
  \end{minipage}%
}%
}

\begin{document}

\lipsum[1-4]

\end{document}

which results in

wrong flushing

What might be the cause of the problem? Is there another package that is better suited?

Best Answer

You have to set \xshift like this

\setlength\xshift{\dimexpr0.5\paperwidth-\marginparwidth-\marginparsep-\oddsidemargin-1in\relax}

Full code:

\documentclass{article}

\usepackage{lipsum,showframe}

\newlength\xshift
\setlength\xshift{\dimexpr0.5\paperwidth-\marginparwidth-\marginparsep-\oddsidemargin-1in\relax}
%\addtolength\xshift{-\marginparwidth}
%\addtolength\xshift{-\marginparsep}
%\addtolength\xshift{-1.0in} % half the minipage width
%\addtolength\xshift{-1pt} % frame width

\usepackage{xcolor}
\usepackage[printwatermark]{xwatermark}
\newwatermark[
pages=1,
color=black,
scale=1,
xpos=\xshift,
ypos=0.0in
]{%
  \fbox{%
  \begin{minipage}{2.0in}\normalsize%
    my awesome minipage\\
    my awesome minipage\\
    my awesome minipage
  \end{minipage}%
}%
}

\begin{document}

\lipsum[1-4]

\end{document}

enter image description here