3

I am trying to create a tikz image that has rounded corners and transparency at the same time. The problem I face with the MWE given below is I can have either transparency or rounded corners, but not both together.

MWE

\documentclass[border=10pt,multi,tikz]{standalone}
\begin{document}
\begin{tikzpicture}
    \node [inner sep=0pt,opacity=0.2,clip,rounded corners=0.1cm] {\includegraphics[width=0.85\textwidth,keepaspectratio]{example-image-a}};
\end{tikzpicture}
\end{document}

How to get both transparency and rounded corners without using additional packages?

3

The probably simplest option is

\documentclass[border=10pt,multi,tikz]{standalone}
\begin{document}
\begin{tikzpicture}
   \begin{scope}[opacity=0.2]
    \node [inner sep=0pt,clip,rounded corners=0.1cm] {\includegraphics[width=0.85\textwidth,keepaspectratio]{example-image-a}};
   \end{scope}  
\end{tikzpicture}
\end{document}

enter image description here

I never understood why there is this restriction because, as seen above, TikZ actually can use such paths for clipping.

However, there are situations in which a simple scope may not be sufficient. In these you can save and reuse the path, and according to my experience that works. (I increased the radius of the rounded corners to make it very visible that it does work)

\documentclass[border=10pt,multi,tikz]{standalone}
\makeatletter % https://tex.stackexchange.com/a/38995/121799
\tikzset{
  use path/.code={\pgfsyssoftpath@setcurrentpath{#1}}
}
\makeatother
\begin{document}
\begin{tikzpicture}
    \node [inner sep=0pt,opacity=0,rounded corners=0.3cm,save path=\mypath]
     {\includegraphics[width=0.85\textwidth,keepaspectratio]{example-image-a}};
    \clip[use path=\mypath]; 
    \node [inner sep=0pt,opacity=0.2]
     {\includegraphics[width=0.85\textwidth,keepaspectratio]{example-image-a}};
\end{tikzpicture}
\end{document}

enter image description here

I agree that all this extra effort should not be necessary but at least it works and satisfies "no extra packages".

If you do not like the \makeatletter stuff, you could use (I learned this from Kpym in a comment)

\documentclass[border=10pt,multi,tikz]{standalone}
\tikzset{use path/.code={\pgfsetpath{#1}}}
\begin{document}
\begin{tikzpicture}
    \node [inner sep=0pt,opacity=0,rounded corners=0.3cm,save path=\mypath]
     {\includegraphics[width=0.85\textwidth,keepaspectratio]{example-image-a}};
    \clip[use path=\mypath]; 
    \node [inner sep=0pt,opacity=0.2]
     {\includegraphics[width=0.85\textwidth,keepaspectratio]{example-image-a}};
\end{tikzpicture}
\end{document}

And yet another option which does not use an explicit clip (but should be used with care) is

\documentclass[border=10pt,multi,tikz]{standalone}
\begin{document}
\begin{tikzpicture}
    \node [inner sep=0pt,rounded corners=0.3cm,
    path picture={\node[opacity=0.2] at (path picture bounding box.center)
    {\includegraphics[width=0.85\textwidth,keepaspectratio]{example-image-a}};}]
{\phantom{\includegraphics[width=0.85\textwidth,keepaspectratio]{example-image-a}}};
\end{tikzpicture}
\end{document}

That is, a path picture effectively clips, which brings us back to the statement that there might be no deep reason for the complaint "no extra options are allowed" at least for harmless options.

  • Should we report to TikZ/PGF team this situation as a kind of "bug"? – Black Mild May 8 at 14:54
  • @BlackMild I am not sure it is a bug because there are situations where the restrictions are perhaps reasonable. The same statement applies to \tikzset{clip even odd rule/.code={\pgfseteorule}}, which can be found here, and in my experience it just works fine. Most likely there is something that can go wrong somewhere, and the author(s) were just very conservative. – user121799 May 8 at 15:03

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.