我想让比周围文本小的 tcolorbox 位于右页边距。有什么办法吗?
为了清楚起见,我附上了当前的 MWE 和一张截图:
\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}
答案1
您可以使用选项before
并在环境after
内打印它flushright
:
\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}
顺便说一句:为了居中,您可以使用center
而不是flushright
。
几乎相同,但上方和下方的垂直空间较少,可以使用选项完成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}
但是如果您不想要上面和下面额外的垂直空间,您也可以使用before=\hfill
或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}