3

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,

  • please provide a complete example which is compilable! What have you tried so far? This post is quite vague. Why do you need transparency if you are putting the text above that? You should search this side for mdframed – LaRiFaRi Sep 5 '14 at 11:20
  • 1
    My usual recommendation: tcolorbox package ;-) – user31729 Sep 5 '14 at 11:42
5

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.

  • nice answer, thanks for that, could you add please the transparency option for tcolorbox and also to automatic fit the box to the text length? I have three lines and the box is always bigger than the longer text. – Gery Sep 5 '14 at 14:06
  • @Gery Transparency for both frame and background added in the third code and width is \textwidth. – user11232 Sep 5 '14 at 14:10
  • haha thank you!! nicely done, total genius!! 8-) – Gery Sep 5 '14 at 17:44
  • one more question, to offset 1 cm to the left the whole box (ie. going beyond the textwidth), is that possible with tcolorbox? – Gery Sep 5 '14 at 18:16
  • MEGA-Like!! RE-G-E-N-I-U-S!!!! 8-)p thanks again!! – Gery Sep 5 '14 at 22:34

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.