TikZ - 如何为具有角落形状的节点创建样式?

TikZ - 如何为具有角落形状的节点创建样式?

这可能是一个愚蠢的问题,但我目前不知道如何为角落创建样式定义。我想要的包含在 MWE 中。

平均能量损失

\documentclass[
a4paper
]{scrartcl}

\usepackage{
newtxtext,
amsmath,
}
\usepackage[T1]{fontenc}
\usepackage[utf8]{inputenc}

\usepackage{tikz}
\tikzset{
testtest/.style={
%rectangle, %don't want this
%circle, %don't want this either
draw=gray,
thick
},  
}

\listfiles

\begin{document}
Test.
\begin{center}
\begin{tikzpicture}[font=\sffamily\small]
\node[testtest] at (0,0) {Some words.};
\end{tikzpicture}
\end{center}
\begin{center}
    \begin{tikzpicture}
        \draw[thick] (0,0) -- (2,0) -- (2,0.5) node[below left] {This is what I want. Just a corner. Its size is going to get adapted anyway.};
    \end{tikzpicture}
\end{center}
\end{document}

图片

在此处输入图片描述

答案1

您可以使用append after command。这是您修改后的 MME:

\documentclass[
a4paper
]{scrartcl}

\usepackage{
newtxtext,
amsmath,
}
\usepackage[T1]{fontenc}
\usepackage[utf8]{inputenc}

\usepackage{tikz}
\tikzset{
testtest/.style={
thick,
append after command={
    [thick,shorten >=-0.2bp, shorten <=-0.2bp]
    (\tikzlastnode.south east)edge(\tikzlastnode.north east)edge(\tikzlastnode.south west)
}

},
}

\listfiles

\begin{document}
Test.
\begin{center}
\begin{tikzpicture}[font=\sffamily\small]
\node[testtest] at (0,0) {Some words.};
\end{tikzpicture}
\end{center}
\begin{center}
    \begin{tikzpicture}
        \draw[thick] (0,0) -- (2,0) -- (2,0.5) node[below left] {This is what I want. Just a corner. Its size is going to get adapted anyway.};
    \end{tikzpicture}
\end{center}
\end{document}

在此处输入图片描述

答案2

下一个代码与 Tahtisilma 的代码类似,但带有一个参数来修复角落的起始位置。

\documentclass[tikz,border=3mm]{standalone}

\usepackage{
newtxtext,
amsmath,
}
\usepackage[T1]{fontenc}
\usepackage[utf8]{inputenc}

\usetikzlibrary{positioning}

\usepackage{tikz}
\tikzset{
corner/.style={
append after command={%
    \pgfextra 
        \path (\tikzlastnode.south west)--(\tikzlastnode.south east) coordinate [pos=#1] (aux); 
        \draw[gray,thick] (aux)-|(\tikzlastnode.north east);
    \endpgfextra
}
},  
}

\begin{document}
    \begin{tikzpicture}
        \node[corner=.25] (A) {This is what I want. Just a corner. Its size is going to get adapted anyway.};
        \node[corner=.5, below=of A] (B) {This is what I want. Just a corner. Its size is going to get adapted anyway.};
        \node[corner=.60, below=of B] (C) {This is what I want. Just a corner. Its size is going to get adapted anyway.};
    \end{tikzpicture}
\end{document}

在此处输入图片描述

如果你不想在里面有一个角落TikZ,而是在一些常规文本中tcolorbox还有另一种可能性:

\documentclass{article}

\usepackage{
newtxtext,
amsmath,
lipsum
}
\usepackage[T1]{fontenc}
\usepackage[utf8]{inputenc}

\usepackage[skins]{tcolorbox}

\newtcbox{\mycorner}[1][.5]{enhanced,nobeforeafter,
tcbox raise base,
arc=0pt, outer arc=0pt, opacityback=0, opacityframe=0, 
boxsep=0pt, left=0pt, right=0pt, top=0pt, bottom=0pt,
overlay = {\path (frame.south west)--(frame.south east) coordinate[pos=#1] (mycorneraux); \draw (mycorneraux)-|(frame.north east);}
}

\begin{document}

This is what I want. Just a corner. \mycorner{Its size is going to get adapted anyway.}

This is what I want. Just a corner. Its size is going \mycorner[.6]{to get adapted anyway.}

This is what I want. Just a corner. Its size is going to get adapted \mycorner[.8]{anyway.}

\end{document}

在此处输入图片描述

相关内容