如何设置自定义node样式的中文位置

2019-11-12 00:46发布

[图]如下图1所示红框是man结点的定义,蓝框是man结点的调用 输出如下图2(请忽视人裆下的小棍,是其他代码产生的) 问题: 我想让系统管理员的字居中显示在小人的脚下。或者说,我想让man结点无论输入什...

如下图1所示
红框是man结点的定义,蓝框是man结点的调用

输出如下图2(请忽视人裆下的小棍,是其他代码产生的)

问题:

  1. 我想让“系统管理员”的字居中显示在小人的脚下。或者说,我想让man结点无论输入什么文字都能居中显示在小人脚下。要怎么修改man的定义呢?
  2. 我还想让这个man结点在后期添加线段的时候不会总是从原点(0,0)开始连接,而是像上图2裆下的小棍那样,离小人有较大的距离(距离大得足以将中文放进去),如下图3所示。

代码如下:(略有删改,输出与截图有所出入,但不影响本问题的提出)

\documentclass{article}
\usepackage{xeCJK}
\usepackage{amsmath, amssymb, graphics, setspace,palatino}
\usepackage{tikz,bm,color,tikz-3dplot,pst-3dplot,pgfplots}
\usepackage[margin=0cm,nohead]{geometry}
\usepackage[active,tightpage]{preview}
\usetikzlibrary{calc}
\usetikzlibrary{shapes,arrows}
\usetikzlibrary{spy}

\PreviewEnvironment{tikzpicture}

\begin{document}
  \makeatletter
  \pgfdeclareshape{man}{
    \savedanchor{\center}{%
      \pgfpointorigin}
    \anchor{center}{\center}
    \backgroundpath{
      \edef\@temp{%
        \noexpand\node[circle,draw] (\tikz@fig@name-头) at (0,0.4) {};
        \noexpand\draw (-0.25,0) -- (0.25,0);
        \noexpand\draw (0,-0.25) -- (0,0.25);
        \noexpand\draw (0,-0.25) -- (-0.25,-0.5);
        \noexpand\draw (0,-0.25) -- (0.25,-0.5);
      }
      \@temp
    }
  }

  \begin{tikzpicture}[> = stealth, node distance = 12mm, thick, scale = 1]
    \node[man] (START) {系统管理员};
  \end{tikzpicture}
\end{document}
3条回答
正如上一楼所说,自定义 shape 要了解一些更为底层的东西,这种基础的不怎么需要使用特殊锚点(进行标注,对齐等)的图形,使用 pics 就好,这里我给了自定义 shape 的 mwe: ```tex \documentclass[tikz, border=1cm]{standalone} \usepackage{xeCJK} \setCJKmainfont{simkai.ttf} \makeatletter \pgfdeclareshape{man}{ \inheritsavedanchors[from=rectangle] % this is nearly a rectangle \inheritanchorborder[from=rectangle] \inheritanchor[from=rectangle]{center} \backgroundpath{ % get north anchor \pgf@process{\southwest} \pgf@xa=.5\pgf@x \pgf@process{\northeast} \pgf@x=.5\pgf@x \advance\pgf@x by \pgf@xa \pgf@xa=\pgf@x \pgf@ya=\pgf@y \pgfmathsetlength\pgf@xb{\pgf@xa-0.25cm} \pgfmathsetlength\pgf@xc{\pgf@xa+0.25cm} \pgfmathsetlength\pgf@yb{\pgf@ya+0.25cm} \pgfmathsetlength\pgf@yc{\pgf@yb+0.25cm} \pgfpathmoveto{\pgfqpoint{\pgf@xb}{\pgf@ya}} \pgfpathlineto{\pgfqpoint{\pgf@xa}{\pgf@yb}} \pgfpathlineto{\pgfqpoint{\pgf@xc}{\pgf@ya}} \pgfpathmoveto{\pgfqpoint{\pgf@xa}{\pgf@yb}} \pgfpathlineto{ \pgfpointadd{\pgfqpoint{\pgf@xa}{\pgf@yb}} {\pgfqpoint{0cm}{.5cm}} } \pgfpathmoveto{\pgfqpoint{\pgf@xb}{\pgf@yc}} \pgfpathlineto{\pgfqpoint{\pgf@xc}{\pgf@yc}} \pgfpathcircle{\pgfpointadd {\pgfqpoint{\pgf@xa}{\pgf@yc}} {\pgfqpoint{0cm}{0.45cm}} }{0.2cm} } } \makeatother \begin{document} \begin{tikzpicture} \node[man, draw] {系统管理员}; \end{tikzpicture} \end{document} ``` ![](https://pics.latexstudio.net/data/images/201911/fa27176dd6f31e4.png) 看了下你复制的内容,很明显自定义 shape 的方式不适合你,即使用了别人的代码,出了问题你也不知道在哪儿,反之 pics 容易上手的多

一周热门 更多>