0

I'm new to tikz and I guess this is a very easy problem. Still I couldn't find the solution.

I want to use tikz to draw a background for my headlines. Therefore I created the following command:

\documentclass{article}
\usepackage{tikz}
\usepackage{xcolor}
\usetikzlibrary{calc}

\newcommand{\headline}[1]{%
    \begin{tikzpicture}
        \fill[gray] (0,2mm) -- (5mm-2mm,2mm) -- (5mm,0) -- (2mm,0) -- (0,2mm);
        \node (A) at (12mm,1mm) {\Large\textsc{#1}};
        \fill[gray] (A.east|-0,2mm) -- (\textwidth-2mm,2mm) -- (\textwidth,0) -- (A.east|-0,0) -- (A.east|-0,0);
    \end{tikzpicture}%
}

\begin{document}
    \headline{test}
\end{document}

The result is the following: Result of \headline command

But I want the left bottom corner of the second line behind the text also to be cut off. Like in this image:

Hoped result

I tried to somehow add the missing 2mm but I didn't find a solution. I tried ([xshift=2mm]A.east|-0,2mm), (A.east|-0+2mm,2mm), \fill[gray] let \p{A}=(A) in (A.east|-0,2mm) -- (\textwidth-2mm,2mm) -- (\textwidth,0) -- (\x{A}+2mm,0) -- (A.east|-0,0); and many other things I came across when searching. But I didn't get it to work.


How do I add the missing 2mm relative to the end of the node?

3

Use the tikz-library calc to calculate the missing coordinate as ($(A.east|-0,0)+(2mm,0)$).

enter image description here

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

\newcommand{\headline}[1]{%
    \begin{tikzpicture}
        \fill[gray] (0,2mm) -- (5mm-2mm,2mm) -- (5mm,0) -- (2mm,0) -- cycle;
        \node (A) at (12mm,1mm) {\Large\textsc{#1}};
        \fill[gray] (A.east|-0,2mm) -- (\textwidth-2mm,2mm) -- (\textwidth,0) -- ($(A.east|-0,0)+(2mm,0)$) -- cycle;
    \end{tikzpicture}%
}

\begin{document}
    \noindent
    \headline{test}
\end{document}
2
  • Works perfectly, thank you. Also thank you for cycle. Just one more question: Why/When do I need the $ signs? – miile7 Jan 30 at 15:48
  • 1
    @miile7 The $ signs indicate that the calc library is to be used for coordinate calculations (in our case, addition). For details, see section 13.5 Coordinate Calculations of the tikz manual. – gernot Jan 30 at 15:54

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.