在 Latex 中创建二维数字线

在 Latex 中创建二维数字线

我想要创建一维和二维的标准化数轴,如下图所示:

数轴示例

我尝试使用 tikz,但有很多东西我都不熟悉。如何使 <- 为空,... ->,如何在四个字段中创建数学表达式,...

答案1

用户对此类问题不太感兴趣的原因之一是,它们迫使可能回答的人从屏幕截图中输入文本。这阻止了许多人回答。因此,以后,一定要发布你尝试过的内容,至少这会让其他人只需复制这些文本即可。这个答案定义了一种风格,通过这种风格,绘制数字线变得像说

\draw[ultra thick,numberline={items={occupied/0.4,don't know/0.6}}] (0,3) -- (5,3);

它具有多种样式和键,可让您通过多种方式自定义外观。

\documentclass[tikz,border=3.14mm]{standalone}
\usetikzlibrary{decorations.pathreplacing,calc,patterns}
\begin{document}
\begin{tikzpicture}[scale=1.2,numberline/.style={/utils/exec=\tikzset{numberline/.cd,#1},postaction={decorate,
 decoration={show path construction,
      lineto code={
       \draw (\tikzinputsegmentfirst) -- (\tikzinputsegmentlast); 
       \edef\mylist{\pgfkeysvalueof{/tikz/numberline/items}}
       \pgfmathsetmacro{\mysum}{0}
       \foreach \XX/\YY in \mylist
       {\pgfmathsetmacro{\mysum}{\mysum+\YY}
       \xdef\mysum{\mysum}}
       \draw[/tikz/numberline/tick]   (\tikzinputsegmentfirst)    
        -- ($(\tikzinputsegmentfirst)!\pgfkeysvalueof{/tikz/numberline/tick length}!-90:(\tikzinputsegmentlast)$) 
        coordinate[pos=0.5] (numberline-0) 
        let \p1=($($(\tikzinputsegmentfirst)!\pgfkeysvalueof{/tikz/numberline/tick
        length}!-90:(\tikzinputsegmentlast)$)-(\tikzinputsegmentfirst)$),\n1={atan2(\y1,\x1)-180} in 
        node[anchor=\n1]{$0$};
       \pgfmathsetmacro{\sumsofar}{0}
       \foreach \XX/\YY [count=\ZZ] in \mylist
       {\pgfmathsetmacro{\sumsofar}{\sumsofar+\YY}
       \xdef\sumsofar{\sumsofar}
       \pgfmathsetmacro{\fraction}{\sumsofar/\mysum}
       \draw[/tikz/numberline/tick] ($(\tikzinputsegmentfirst)!\fraction!(\tikzinputsegmentlast)$) -- 
        ($(\tikzinputsegmentfirst)!\fraction!(\tikzinputsegmentlast)!\pgfkeysvalueof{/tikz/numberline/tick length}!90:(\tikzinputsegmentfirst)$)
        coordinate[pos=0.5] (numberline-\ZZ) 
        let \p1=($($(\tikzinputsegmentfirst)!\pgfkeysvalueof{/tikz/numberline/tick
        length}!-90:(\tikzinputsegmentlast)$)-(\tikzinputsegmentfirst)$),\n1={atan2(\y1,\x1)-180} in
        node[anchor=\n1]{$\pgfmathprintnumber{\fraction}$};
       \draw[/tikz/numberline/arrow] (numberline-\the\numexpr\ZZ-1) -- (numberline-\the\numexpr\ZZ) node[/tikz/numberline/node]{\XX};
       }
      }}}},numberline/.cd,tick/.style={ultra thin},arrow/.style={latex-latex,thin},node/.style={allow upside down=true,auto,swap,sloped,midway,font=\small\sffamily},items/.initial={A/1,B/1},
      tick length/.initial=7pt]
 \draw[ultra thick,numberline={items={occupied/0.4,don't know/0.6}}] (0,3) -- (5,3);
 \draw[ultra thick,numberline={items={occupied/0.6,don't know/0.4}}] (0,0) -- (5,0);
 \begin{scope}[xshift=7cm,pft/.style={align=center,font=\sffamily\small,inner sep=0.5pt,outer sep=0pt}]
  \draw[thin,pattern=dots] (0,0) rectangle (3,2) node[midway,pft]{Occupied $\Omega$\\ $=$occupied} 
    (0,2) rectangle (3,5) node[midway,pft]{Occupied $\Omega$\\ $=$duck}  (3,0) rectangle (5,2) node[midway,pft]{Don't know $\Omega$\\ $=$koala} ;
  \draw[thin,pattern=crosshatch dots]  (3,2) rectangle (5,5) node[midway,pft,fill=white]{Don't know $\Omega$\\ $=$squirrel} ;
  \draw[ultra thick,numberline={items={occupied/0.6,don't know/0.4}}] (0,0) -- (5,0);
  \draw[ultra thick,numberline={items={occupied/0.4,don't know/0.6},tick length=-7pt,node/.append style={above}}] (0,0) -- (0,5);
 \end{scope}
\end{tikzpicture}
\end{document}

在此处输入图片描述

相关内容