4

Here is my MWE taken from here:

\documentclass[varwidth]{standalone}
\usepackage{ eso-pic,tikz, lipsum}
\usetikzlibrary{tikzmark}
\linespread{2}
\newcommand\BackgroundPic{%
\begin{tikzpicture}[overlay,remember picture]
\node[anchor=north west,inner sep=0pt, rounded corners=10pt] at ([yshift=8pt]pic cs:start) {%
\includegraphics[width=\textwidth]{example-image}};
\end{tikzpicture}}

\begin{document}
\AddToShipoutPicture*{\BackgroundPic}

\tikzmark{start}\lipsum[1-1]\tikzmark{end}

\end{document}

I would like the background image to be with rounded corners. But it does not work.

How do I get the background image with rounded corners?

deshmukh
  • 2,435
  • 1
  • 26
  • 46

2 Answers2

5

Use clip along with rounded corners while including image.

enter image description here

\documentclass[varwidth,border=10pt]{standalone}
\usepackage{calc}
\usepackage{ eso-pic,tikz, lipsum}
\usetikzlibrary{tikzmark}
\linespread{2}
\newcommand\BackgroundPic{%
\begin{tikzpicture}[overlay,remember picture]
\node[anchor=north west,inner sep=0pt, clip, rounded corners=20pt] at ([yshift=15pt,xshift=-5pt]pic cs:start) {%
\includegraphics[width=\textwidth+10pt]{example-image}};
\end{tikzpicture}}

\begin{document}
\AddToShipoutPicture*{\BackgroundPic}   
\tikzmark{start}\lipsum[1-1]\tikzmark{end}   
\end{document}
nidhin
  • 7,932
  • Thanks. This is almost there. But part of the text goes outside. Is there a way to ensure that the text remains strictly inside the image? – deshmukh Dec 09 '18 at 12:26
  • @deshmukh It is because the image height is less than that of the text. Either an image with height matching with text has to be used or the current image has to be stretched/zoomed. – nidhin Dec 09 '18 at 12:33
  • @deshmukh you can mention height= along with width= in includegraphics. But this can alter the aspect ratio. – nidhin Dec 09 '18 at 12:40
  • No, I tried with a very large image 744*1052 and text does go outside the rounded corners – deshmukh Dec 09 '18 at 12:41
  • @deshmukh We are scaling the image such that the with is equal to textwidth. So the image height with respect to its width matters not the absolute height. Try with another image having a height more than its width. Say 1052 *744 :) – nidhin Dec 09 '18 at 12:47
  • Thanks. I did. Still the same problem. The text spills beyond the rounding of corners. – deshmukh Dec 09 '18 at 12:50
  • 1
    Thanks. This works perfectly. I accepted the other answer, however, as it was more succinct. – deshmukh Dec 14 '18 at 12:10
2

Another solution with tcolorbox. As blankest option suppresses rounded corners, its effects have been simulated with boxsep=0t and other geometry parameters.

\documentclass[tikz, border=2mm]{standalone}
\usepackage{graphicx, lipsum}
\usepackage[most]{tcolorbox}

\newtcolorbox{myminipage}[3][]{
     enhanced,
%    blankest, 
%    rounded corners,
     boxsep=0pt,
     left=0pt,
     right=0pt,
     top=0pt,
     bottom=0pt,
    width=#2,
    underlay={\begin{tcbclipframe}
    \node at (frame) {\includegraphics{#3}};
    \end{tcbclipframe}},
    #1
}

\begin{document}

\begin{myminipage}{3cm}{example-image}
This is some text not so long like \texttt{\textbackslash{}lipsum[2]}
\end{myminipage}

\begin{myminipage}{10cm}{example-image}
\lipsum[2]
\end{myminipage}
\end{document}

enter image description here

Ignasi
  • 136,588