4

I wish to customize a node with only bottom border drawed. below is my code:

\documentclass{standalone}
\usepackage{tikz}
\makeatletter
\pgfdeclareshape{mybox}{
    \inheritsavedanchors[from=rectangle]
    \inheritanchorborder[from=rectangle]
    \inheritbackgroundpath[from=rectangel]
    \foreach \i in {center,north,south,east,west,south east,south west} {
        \inheritanchor[from=rectangle]{\i}
    }
    \anchor{text} {
        \pgfpoint{-.5\wd\pgfnodeparttextbox}{-.5\ht\pgfnodeparttextbox}
    }
    \behindbackgroundpath{
        \northeast \pgf@xa=\pgf@x \pgf@ya=\pgf@y
        \southwest \pgf@xb=\pgf@x \pgf@yb=\pgf@y
        \pgfusepath{}
        \pgfpathmoveto{\pgfqpoint{\pgf@xa}{\pgf@yb}}%
        \pgfpathlineto{\southwest}%
        \pgfpathclose
    }
    \foregroundpath{
        \pgfusepath{stroke}
    }
}
\makeatother
\begin{document}
    \begin{tikzpicture}
    \node[mybox] (A) {Text here};
    \end{tikzpicture}
\end{document}

Current logic can draw the bottom line but text not show, only one line has been draw on output.

3
  • 1
    There is a typo: \inheritbackgroundpath[from=rectangel] needs to become \inheritbackgroundpath[from=rectangle]. (This does not fix the full issue, though.)
    – user194703
    Nov 4, 2019 at 21:45
  • 1
    I can't reproduce the output you report. If I add draw to the node options, I get a complete rectangle drawn. In any case, I get the text, though it doesn't fit into the shape. If I don't add draw, I get no line drawn at all.
    – cfr
    Nov 4, 2019 at 22:00
  • 1
    The manual explicitly says that \anchor{text}{\pgfpoint{-.5\wd\pgfnodeparttextbox}{-.5\ht\pgfnodeparttextbox}} is wrong (1129) just after saying what should be written.
    – cfr
    Nov 4, 2019 at 22:05

2 Answers 2

2
\documentclass[border=9pt]{standalone}
\usepackage{tikz}
\makeatletter
\pgfdeclareshape{mybox}{
    \inheritsavedanchors[from=rectangle]
    \inheritanchorborder[from=rectangle]
    \inheritbehindbackgroundpath[from=rectangle]
    \inheritforegroundpath[from=rectangle]
    \foreach \i in {center,north,south,east,west,south east,south west} {
        \inheritanchor[from=rectangle]{\i}
    }
    \inheritbeforeforegroundpath[from=rectangle]
    \inheritbehindforegroundpath[from=rectangle]
    \savedanchor{\upperrightcorner}{% 1127
      \pgf@yc=.5\ht\pgfnodeparttextbox % height of the box, ignoring the depth
      \pgf@xc=.5\wd\pgfnodeparttextbox % width of the box
    }
%     \anchor{text} {% manual explicitly says this is wrong (1129)
%         \pgfpoint{-.5\wd\pgfnodeparttextbox}{-.5\ht\pgfnodeparttextbox}
%     }
    \anchor{text}{% 1129
      \upperrightcorner%
      \pgf@x=-\pgf@xc%
      \pgf@y=-\pgf@yc%
    }
    \anchor{center}{\pgfpointorigin}
    \backgroundpath{
        \northeast \pgf@xa=\pgf@x \pgf@ya=\pgf@y
        \southwest \pgf@xb=\pgf@x \pgf@yb=\pgf@y
        \pgfpathmoveto{\pgfpoint{\pgf@xb}{\pgf@ya}}%
        \pgfpathlineto{\pgfpoint{\pgf@xb}{\pgf@yb}}%
        \pgfpathlineto{\pgfpoint{\pgf@xa}{\pgf@yb}}%
        \pgfpathlineto{\pgfpoint{\pgf@xa}{\pgf@ya}}%
        \pgfpathmoveto{\pgfpoint{\pgf@xb}{\pgf@ya}}%
      }
}
\makeatother
\begin{document}
    \begin{tikzpicture} 
    \node[mybox,draw] (A) {Text here};
  \end{tikzpicture}
\end{document}

open box

1
  • 1
    @Schrödinger'scat I just took that from the OP's code. I assumed (maybe wrongly) that those were the anchors the OP wanted. However, you're of course right that the list isn't complete.
    – cfr
    Nov 4, 2019 at 23:01
2

Do you need a new shape for this? I think a path picture can solve the problem

\documentclass[border=2mm, tikz]{standalone}
\usetikzlibrary{positioning}

\begin{document}
\begin{tikzpicture}[
    mybox/.style={path picture={\draw[#1] (path picture bounding box.south west)--(path picture bounding box.south east);}}]

    \node[mybox] (a) {Text here};
    \node[mybox=red, right= of a] (b) {Another text here};
    \node[mybox={ultra thick, blue}, right= of b] (c) {And another one here};
\end{tikzpicture}
\end{document}

enter image description here

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .