Tcolorbox – How to Move a Tcolorbox to the Right Side of the Page in LaTeX

tcolorbox

I would like to have my tcolorbox, which is smaller than the surrounding running text, be positioned towards the right page margin. Is there a way to do this?

I've included my current MWE and a screenshot of what I mean for clarity:

\documentclass[a4paper]{article}

\usepackage{tcolorbox}
\usepackage{fontawesome5}
\usepackage{lipsum}
% ---------------------------------------------------------------------------- %

\newtcolorbox{importantbox}[2][]{
  width=0.9\textwidth,
  colback=white, 
  colframe=yellow, 
  coltitle=black, 
  fonttitle=\bfseries, 
  title=\faIcon{exclamation-triangle}~Caution: #2,#1
}

% ---------------------------------------------------------------------------- %
\begin{document}

\lipsum

\begin{importantbox}{Lorem Ipsum}
   \lipsum
\end{importantbox}

\end{document}

what I'm trying to do with the tcolorbox

Best Answer

You can use options before and after to print it inside a flushright environment:

\documentclass[a4paper]{article}

\usepackage{tcolorbox}
\usepackage{fontawesome5}
\usepackage{lipsum}
% ---------------------------------------------------------------------------- %

\newtcolorbox{importantbox}[2][]{
  before=\begin{flushright},
  after=\end{flushright},
  width=0.9\textwidth,
  colback=white, 
  colframe=yellow, 
  coltitle=black, 
  fonttitle=\bfseries, 
  title=\faIcon{exclamation-triangle}~Caution: #2,#1
}

% ---------------------------------------------------------------------------- %
\begin{document}

\lipsum[1-2]

\begin{importantbox}{Lorem Ipsum}
   \lipsum[3]
\end{importantbox}

\lipsum[4]

\end{document}

enter image description here

BTW: For centering you can use center instead of flushright.

Almost the same, but with less vertical space above and below, can be done using option flush right:

\documentclass[a4paper]{article}

\usepackage{tcolorbox}
\usepackage{fontawesome5}
\usepackage{mwe}
% ---------------------------------------------------------------------------- %

\newtcolorbox{importantbox}[2][]{
  flush right,
  width=0.9\textwidth,
  colback=white, 
  colframe=yellow, 
  coltitle=black, 
  fonttitle=\bfseries, 
  title=\faIcon{exclamation-triangle}~Caution: #2,#1
}

% ---------------------------------------------------------------------------- %
\begin{document}

\lipsum[1-2]

\begin{importantbox}{Lorem Ipsum}
   \lipsum[3]
\end{importantbox}

\lipsum[4]

\end{document}

enter image description here

But if you don't want the extra vertical space above and below, you can alternatively use before=\hfill or before=\hspace*{\fill}:

\documentclass[a4paper]{article}

\usepackage{tcolorbox}
\usepackage{fontawesome5}
\usepackage{mwe}
% ---------------------------------------------------------------------------- %

\newtcolorbox{importantbox}[2][]{
  before=\hfill,
  width=0.9\textwidth,
  colback=white, 
  colframe=yellow, 
  coltitle=black, 
  fonttitle=\bfseries, 
  title=\faIcon{exclamation-triangle}~Caution: #2,#1
}

% ---------------------------------------------------------------------------- %
\begin{document}

\lipsum[1-2]

\begin{importantbox}{Lorem Ipsum}
   \lipsum[3]
\end{importantbox}

\lipsum[4]

\end{document}

enter image description here

Related Question