使用 tikz 更好的時間軸圖

使用 tikz 更好的時間軸圖

我想畫一個盡可能接近下面手繪圖的圖。

在此輸入影像描述

我已嘗試以下操作:

\documentclass[a4paper, 12 pt]{article}
\usepackage{tikz}
\usetikzlibrary{positioning}
\usepackage{enumitem}

    \begin{document}
          \begin{figure}
        \setlist[itemize]{nosep, leftmargin=*}
        \begin{tikzpicture}[
            node distance = 0mm and 0.02\linewidth,
            box/.style = {inner xsep=0pt, outer sep=0pt,
                text width=0.32\linewidth,
                align=left, font=\small}
            ]
            \node (n1) [box]
            {   \begin{itemize}
                    \item   Households can produce or consume $x$ with equal probability
                    \item Young entrepreneurs borrow bank deposits to buy $x$ for investment
                    \item Young bankers issue deposits and loans
                \end{itemize}
            };
            \node (n2) [box, below right=of n1.north east]
            {   \begin{itemize}
                    \item  $\frac{1}{4}$ probability of type $l$ consumers
                    \item  $\frac{1}{4}$ probability of type $h$ consumers
                    \item  $\frac{1}{2}$ probability of producers
                \end{itemize}
            };
            \node (n3) [box, below right=of n2.north east]
            {   \begin{itemize}
                    \item   Old entrepreneurs get output, repay loans, consume $x$ and die
                    \item   Old bankers receive loan repayments, consume $x$ and die
                \end{itemize}
            };
            \draw[thick, -latex]    (n1.north west) -- (n3.north east);
            \draw (n1.north) -- + (0,3mm) node[above] {Day};
            \draw (n2.north) -- + (0,3mm) node[above] {Night};
            \draw (n3.north) -- + (0,3mm) node[above] {Day};
        \end{tikzpicture}
      \end{figure}
    \end{document}

上面的程式碼產生這個圖: 在此輸入影像描述

這和我想要的不一樣。我想包括 $t$ 和 $t+1$,並且還讓刻度線穿過該線,如果有意義的話,它們將顯示在該線上的中心?另外,可能會更好地在線上方添加一些文字區塊,就像它們出現在手繪圖上一樣。

答案1

這是一種由 PGFKeys 支援的方法,\tikztimeline需要一個條目列表,您可以為列表中的每個元素設定一個或全部四個值top, above, below, 。bottom

您可以使用\tikztimelineset在全域範圍內調整預設值,或使用可選參數\tikztimeline僅為一個圖表設定值。

長度segment widthtick length可以調整。

您將確保segment width× 個條目對於您的頁面來說不會太長。

程式碼

\documentclass[a4paper, 12pt]{article}
\usepackage{tikz}
\usetikzlibrary{arrows.meta}
\newcommand*\tikztimelineset{\pgfqkeys{/tikz/timeline}}
\tikztimelineset{
  segment width/.initial=4cm, tick length/.initial=6pt,
  topbot/.style={
    align=flush left,
    text width=\pgfkeysvalueof{/tikz/timeline/segment width}
           -2*(\pgfkeysvalueof{/pgf/inner xsep})},
  top/.initial=, above/.initial=, below/.initial=, bottom/.initial=}
\newcommand*\tikztimeline[2][]{
\begin{tikzpicture}[timeline/every timeline/.try,timeline/.cd,#1]
\foreach[count=\CNT] \elem in {#2}{
  \tikztimelineset{/tikz/style/.expand once=\elem}
  \tikzset{xshift=\CNT*(\pgfkeysvalueof{/tikz/timeline/segment width})}
  \unless\ifnum\CNT=1\relax
  \draw (down:\pgfkeysvalueof{/tikz/timeline/tick length}) -- coordinate (lasttick)
        (  up:\pgfkeysvalueof{/tikz/timeline/tick length});
  \fi
  \node[above] (@above) at (right:{.5*(\pgfkeysvalueof{/tikz/timeline/segment width})})
    {\strut$\pgfkeysvalueof{/tikz/timeline/above}$};
  \node[above, timeline/topbot] at (@above.north) {\pgfkeysvalueof{/tikz/timeline/top}};
  \node[below] (@below) at (@above.south) {\strut\pgfkeysvalueof{/tikz/timeline/below}};
  \node[below, timeline/topbot] at (@below.south) {\pgfkeysvalueof{/tikz/timeline/bottom}};
}
\draw[->] (right:\pgfkeysvalueof{/tikz/timeline/segment width})
      -- ([xshift=\pgfkeysvalueof{/tikz/timeline/segment width}] lasttick);
\end{tikzpicture}}

\tikztimelineset{every timeline/.append style={>=Latex}}

\begin{document}
\tikztimeline{
  {
    top=Households can produce or consume $x$ with equal probabilit.,
    above=t,
    below=Day,
    bottom=Young entrepreneurs borrow bank deposits to buy $x$ for investment.\\
           Young bankers issue deposits and loans.
  }, {
    top=$\frac{1}{4}$ probability of type $l$ consumers.
        $\frac{1}{4}$ probability of type $h$ consumers.
        $\frac{1}{2}$ probability of producers.,
    below=Night
  }, {
    above=t+1,
    below=Day,
    bottom={Old entrepreneurs get output, repay loans, consume $x$ and die.\\
            Old bankers receive loan repayments, consume $x$ and die.}
  }}
\end{document}

輸出

在此輸入影像描述

相關內容