居中节点名称

居中节点名称

我想绘制一个包含罗马数字的正方形节点的图表。它必须足够大,以便数字 VIII 可以容纳,并且我希望所有正方形的大小相同。到目前为止我做到了,不幸的是我的罗马数字没有居中。有人能告诉我如何将它们居中吗?

\documentclass{standalone}
\usepackage{tikz}
\usetikzlibrary{shapes}
\newcommand{\rom}[1]{\uppercase\expandafter{\romannumeral #1\relax}}
%\tikzset{square/.style={regular polygon,regular polygon sides=4,inner sep=0}}
\begin{document}    
 \begin{tikzpicture}[
    square/.style={
    draw,
    regular polygon, regular polygon sides=4,
    text width={width("\rom{1}")},
    %text width={width("\rom{8}")},
    align=center,
    font=\small}]
\node[square] (iv) at (0,2) {\rom{4}};
\node[square] (v) at (0,1) {\rom{5}};
\node[square] (vi) at (0,0) {\rom{6}};
% \node[shape=rectangle,draw=black] (viii) at (3,0) {\rom{8}};

\node[shape=circle,draw=black] (0) at (2,2) {0};

\node at (0,0) [square,draw] (viii) {\rom{8}};
\node at (1,0) [square,draw] (i) {\rom{1}};
\node at (2,0) [square,draw] (ii) {\rom{2}};
\end{tikzpicture}   
\end{document}

答案1

您在 处有两个节点(0,0)。这是故意的吗?如果您将节点定位在固定坐标处,它们可能会重叠。这是使用相对定位和 CarLaTeX 建议的提议。

\documentclass{standalone}
\usepackage{tikz}
\usetikzlibrary{shapes,positioning}
\newcommand{\rom}[1]{\uppercase\expandafter{\romannumeral #1\relax}}
%\tikzset{square/.style={regular polygon,regular polygon sides=4,inner sep=0}}
\begin{document}    
 \begin{tikzpicture}[
    square/.style={
    draw,
    regular polygon, regular polygon sides=4,
    %text width={width("\rom{1}")},
    text width={width("\rom{8}")},
    text centered,
    align=center,inner sep=-3pt, %<- you can play with this value
    font=\small}]
\node[square] (iv) at (0,2) {\rom{4}};
\node[square,below=0.2cm of iv] (v)  {\rom{5}};
\node[square,below=0.2cm of v] (vi) {\rom{6}};
% \node[shape=rectangle,draw=black] (viii) at (3,0) {\rom{8}};

\node[shape=circle,draw=black] (0) at (2,2) {0};

\node[square,draw,right=0.2cm of vi]  (viii) {\rom{8}};
\node[square,draw,right=0.2cm of viii] (i) {\rom{1}};
\node[square,draw,right=0.2cm of i] (ii) {\rom{2}};
\end{tikzpicture}   
\end{document}

在此处输入图片描述

相关内容