[Tex/LaTex] text with semi-transparent color filled box

boxesfillbetweentext-decorationstransparency

I have this text:

\begin{flushleft}
    \fontsize{16pt}{16pt}\selectfont\textit{some text}\\
    \fontsize{16pt}{16pt}\selectfont\textit{some text}
\end{flushleft}

and I would like to have a filled box with a semi-transparent color (eg. white or light gray) underlying this text. I've found several examples for beamer, but this is for a report, I assume that beamer is different from report in this sense in Latex.

I appreciate some support to do this, thanks in advance,

Best Answer

With just tikz

\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{positioning}

\begin{document}
  \begin{tikzpicture}
    \node[fill=olive,fill opacity=0.3,rounded
        corners=1ex,font=\fontsize{16pt}{16pt}\itshape] (a) {Some text};
    \node[preaction={fill=olive},rounded corners=1ex,below=of
        a,font=\fontsize{16pt}{16pt}\itshape] (b) {Some text};
    \node[preaction={fill=olive,fill opacity=0.5},rounded corners=1ex,below=of
        b,font=\fontsize{16pt}{16pt}\itshape] (c) {Some text};
  \end{tikzpicture}
\end{document}

enter image description here

With tcolorbox

\documentclass{article}
\usepackage[many]{tcolorbox}

\begin{document}
  \begin{tcolorbox}[
    width=\textwidth,
    arc=3mm,
%    auto outer arc,
    boxsep=0cm,
    toprule=1pt,
    leftrule=1pt,
    bottomrule=1pt,
    rightrule=1pt,
    colframe=blue,
    fontupper=\raggedleft\itshape]
     \fontsize{16pt}{16pt}
     Some text
\end{tcolorbox}%
\end{document}

enter image description here

Here is an environment version with transparency:

\documentclass{article}
\usepackage[many]{tcolorbox}
\usepackage{lmodern}    % just to avoid font size warnings

\newtcolorbox{mybox}[1][]{
    width=\textwidth,
    arc=3mm,
%    auto outer arc,
    boxsep=0cm,
    toprule=1pt,
    leftrule=1pt,
    bottomrule=1pt,
    rightrule=1pt,
    colframe=blue,
    fontupper=\raggedleft\fontsize{16pt}{16pt}\itshape,
    breakable,
    nobeforeafter,
    enhanced jigsaw,
    opacityframe=0.5,
    opacityback=0.5
}

\begin{document}
  \begin{mybox}[]
     Some text here just to fill the line and see if the line breaks smoothly and goes to the next. If not, I am in deep trouble ;-)
  \end{mybox}
\end{document}

enter image description here

This breaks across pages too.