5

I want to draw a graph with square nodes containing roman numerals. It has to be big enough that the number VIII fits in, and I would like to have all squares the same size. I got it so far, unfortunately my roman numerals are not centered. Can someone tell me how to center them?

\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}
2
  • Add to every node option text width=width("\rom{8}"), text centered. You could also create a style.
    – CarLaTeX
    Jan 27, 2018 at 14:00
  • If I write \node at (0,0) [square,draw, text centered] (v100) {\rom{8}}; it is still not centered, nor is it when i include it in the style of the square. And i would like to keep the size as small as it is
    – mabu
    Jan 27, 2018 at 14:27

1 Answer 1

3

You had two nodes at (0,0). Was that on purpose? If you position the nodes at fixed coordinates, they may overlap. Here is a proposal using relative positioning and CarLaTeX's suggestions.

\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}

enter image description here

2
  • Sorry the two nodes at (0,0) were not on purpose.
    – mabu
    Jan 27, 2018 at 15:04
  • @mabu No worries, I was assuming this anyway. That was a very nice first question!
    – user121799
    Jan 27, 2018 at 15:21

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.