如何将文本放在对齐的边缘

如何将文本放在对齐的边缘

我正在寻找一种方法,将文本(或实际上类似于 N 的符号)放在箭头上,使其与箭头对齐。目前我正在手动对齐它,这对于大数字来说很麻烦。以下是一个具有“删除线”的代表性示例,效果很好。谢谢

\documentclass[border=1pt]{standalone}
\def\xcolorversion{2.00}
\def\xkeyvalversion{1.8}

\usepackage[version=0.96]{pgf}
\usepackage{tikz}
\usepackage{amsmath}
\usetikzlibrary{arrows,shapes,snakes,automata,backgrounds,petri}
\usetikzlibrary{shapes.misc}
\usetikzlibrary{decorations,decorations.markings,patterns}
\usepackage[latin1]{inputenc}
\colorlet{darkgray}{black!60}

\tikzset{
  place/.style={
  circle,
  draw=black,
  fill=white,
  inner sep=0pt,
  minimum size=6mm,
  font=\sffamily\scriptsize,
 },
 strike through/.style={
  postaction=decorate,
  decoration={
  markings,
  mark=at position #1 with {
    \draw[-] (-2pt,-2pt) -- (2pt, 2pt);
        }
    }
   }
 }

\begin{document}
\begin{tikzpicture}[node distance=1.3cm,>=stealth',bend angle=45,auto, ->, every node/.style={font=\sffamily\large}]

\begin{scope}
  %P
  \path
  (0,0)  node (Start) [place, tokens=0, label=above:{\footnotesize Start}] {}
  (2,-2)  node (Stop) [place, tokens=0, label=below:{\footnotesize Stop}] {}
  ;
  \path[every node/.style={font=\sffamily\tiny}]
  (Stop) edge [right, pos=0.5, strike through=0.5]  node[above] {\footnotesize $3$} (Start)
  (Start) edge [bend right]  node[midway,above=-0.7em, rotate=-30] {N} (Stop)
  ;
\end{scope}

\end{tikzpicture}
\end{document}

在此处输入图片描述

答案1

您可以sloped在箭头的任意位置使用节点参数。我还用strike through符号替换了$\not$(还有其他替代方案)。

\documentclass{article}
\usepackage{tikz}
\begin{document}
\begin{tikzpicture}[
  >=stealth,
  font=\sffamily,
  node distance=2cm,
  place/.style={
    circle,
    draw=black,
    fill=white,
    inner sep=0pt,
    minimum size=6mm},
  ]
  \path
  (0,0)  node (Start) [place, label=above:{\footnotesize Start}] {}
  (2,-2)  node (Stop) [place, label=below:{\footnotesize Stop}] {}
  ;
  \path
  (Stop) edge[->]  node[pos=0.5,sloped]{$\not$}  node[pos=0.5,anchor=-135,inner sep=1pt] {\footnotesize $3$} (Start)
  (Start) edge [->,bend right=45]  node[pos=0.5,sloped,font=\sffamily\tiny] {N} (Stop)
  ;
\end{tikzpicture}
\end{document}

在此处输入图片描述

答案2

为了好玩,有一个简单的方法可以做到这一点pstricks

\documentclass[border=20pt, svgnames]{standalone}
\usepackage[T1]{fontenc}
\usepackage[utf8]{inputenc}
\usepackage{pst-node}
%\usepackage{auto-pst-pdf} % To compile with pdflatex -shell-escape (TeX Live, MacTeX)%{ mnode = C,
                                                         % pdflatex --enable-write18 (MiKTeX)

\begin{document}

\psset{radius = 0.4cm, linejoin = 1, arrowinset=0.12, arrows =->, shortput = nab, fillstyle = solid, fillcolor = WhiteSmoke, linecolor = SlateGray, labelsep = 3pt}
\sffamily\footnotesize
\begin{psmatrix}[colsep=3cm, rowsep = 2cm]
\Cnode{St}\nput{90}{St}{Start}\\
& \Cnode{Sp}\nput{90}{Sp}{Sto\smash{p}}
% %%% labels and arrows
\psset{linecolor = black}
\ncline{Sp}{St}\nbput[npos = 0.54, labelsep = 0pt]{3}\ncput{\psline[arrows = -, linewidth = 0.5pt](-0.1,0)(0.1,0)}
\ncarc[arcangle =-45, fillstyle = none, fillcolor]{St}{Sp}\ncput*[nrot =-45, fillcolor = white, framesep = 1.5pt]{N}
\end{psmatrix}

\end{document} 

在此处输入图片描述

相关内容