我怎样才能将两个尺寸完全相同的盒子紧挨着放在一起?

我怎样才能将两个尺寸完全相同的盒子紧挨着放在一起?

我是 Latex 和 Tikz 的新手,我正在努力制作两个彼此相邻且尺寸完全相同的盒子。

(因此像这样,但是中间没有间距,并且 123 节点没有变大)

现在我想到了这个:

\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{positioning} 

\usepackage{xstring}



\begin{document}

\begin{tikzpicture}[
    roundnode/.style={circle, draw=blue!60, fill=blue!5, very thick, exact size=5mm},
    squarednode/.style={rectangle, draw=blue!60, fill=blue!5, very thick, minimum size=5mm},
    ]
    %Nodes
    \node[squarednode]      (maintopic)                              {2};
    \node[squarednode]      (mynode)            [right=of maintopic] {123};
    %Connections
    \draw[] (maintopic) (mynode);
\end{tikzpicture}
\end{document}

答案1

在此处输入图片描述

@SebGlav 回答的一个小变化:

\documentclass[border=3mm]{standalone}
%\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{positioning}


\begin{document}

    \begin{tikzpicture}[
node distance = 0pt,
square/.style = {draw=blue!60, fill=blue!5, very thick, 
                 minimum height=1.2em, minimum width=3em, % <---
                 outer sep=0pt},                          % <---
    ]
%Nodes
\node[square]   (maintopic) {2};
\node[square, right=of maintopic] (mynode) {123};
\end{tikzpicture}
\end{document}

答案2

如果您想要方形节点,请使用geometry.shape regular polyogon

\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{positioning, shapes.geometric}

\begin{document}

\begin{tikzpicture}[
    squarednode/.style={regular polygon, regular polygon sides=4, 
    draw=blue!60, fill=blue!5, very thick, minimum size=15mm, inner sep=0pt},
    ]
    %Nodes
    \node[squarednode](maintopic) {2};
    \node[squarednode, anchor=west](mynode) at (maintopic.east)  {123};
    %Connections (which ones? they are close together...
    \draw[] (maintopic) (mynode);
\end{tikzpicture}
\end{document}

在此处输入图片描述

顺便说一句,没有这样的密钥exact size

要设置颜色,您可以将参数传递给样式:

\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{positioning, shapes.geometric}

\begin{document}

\begin{tikzpicture}[
    squarednode/.style={regular polygon, regular polygon sides=4,
    draw=#1!60, fill=#1!5, very thick, minimum size=15mm, inner sep=0pt},
    squarednode/.default=blue,
    ]
    %Nodes
    \node[squarednode=red](maintopic) {2};
    \node[squarednode, anchor=west](mynode) at (maintopic.east)  {123};
    %Connections (which ones? they are close together...
    \draw[] (maintopic) (mynode);
\end{tikzpicture}
\end{document}

在此处输入图片描述

答案3

使用positioning能力和节点anchor

\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{positioning} 

\begin{document}
    

    \begin{tikzpicture}[
        squarednode/.style={rectangle, draw=blue!60, fill=blue!5, very thick, minimum width=\nodewidth},
        ]
        
        \def\nodewidth{5mm}

        \node[squarednode]      (maintopic)                              {2};
        \node[squarednode]      (mynode)            [right= 0.5*\nodewidth of maintopic.center,anchor=west] {123};
        
        \def\nodewidth{10mm}

        \node[squarednode,below=1cm of maintopic.center]      (maintopic2)                              {2};
        \node[squarednode]      (mynode2)            [right= 0.5*\nodewidth of maintopic2.center,anchor=west] {123};
    
    \end{tikzpicture}
\end{document}

节点之间没有空间

答案4

你真的需要用大锤来砸碎坚果吗? 你可以用\fcolorbox以下eqparbox软件包来完成:

\documentclass[12pt]{article}
\usepackage{eqparbox}
\usepackage[svgnames]{xcolor}
\newcommand\eqcolorbox[4][C]{\setlength{\fboxrule}{1.5pt}\setlength{\fboxsep}{5pt}%
\fcolorbox{#2}{#3}{\eqmakebox[#1]{#4}}}

\begin{document}

\eqcolorbox{MediumBlue}{Lavender}{23}\hspace{-\fboxrule}\eqcolorbox{MediumBlue}{Lavender}{12345}

\end{document} 

在此处输入图片描述

相关内容