改进我的 TikZ 代码以制定时间表

改进我的 TikZ 代码以制定时间表

我使用 TikZ 制作了课程表,但我的代码不够优雅,效率低下。大多数步骤都是针对每门课程重复的,所以我想看看如何将它们结合起来。我还想让代码更容易修改,例如,添加新课程、更改课程时间以及更改时间表上显示的时间。我将不胜感激任何改进建议。

\documentclass[12pt]{article}
\usepackage[margin=1.25in]{geometry}
\usepackage{comment}
\pagenumbering{gobble}

\usepackage[dvipsnames]{xcolor}
\definecolor{Pantone113U}{RGB}{255 225 94}
\definecolor{Pantone2347U}{RGB}{231 69 54}
\definecolor{gPurple}{RGB}{117 102 160}
\definecolor{gEvergreen}{RGB}{0 78 66}

\usepackage{tikz}
\usetikzlibrary{arrows,decorations.pathmorphing,backgrounds,positioning,fit,petri,calc}

\begin{document}
        
        {\centering\LARGE\textbf{Fall 2020 Schedule}\\}
        \vspace{0.5in}
                
                \begin{figure}[h]
                    \centering
                    \begin{tikzpicture}
                    \draw[gray] (0,0)   -- ++(5in,0)    -- ++(0,-15cm) -- ++(-5in,0) -- cycle;
                    \draw[gray] (5in, 0) -- (0, 0)
                        node[left,black]{12 p.m.};
                    \foreach \x in {1, ..., 10}
                        \draw[gray] (5in, -1.5*\x) -- (0, -1.5*\x) node[left,black]{\x \space p.m.};
                    
                    \draw[gray] (0,0) -- (1in,0) node[midway,above,black]{Monday} -- (1in, -15cm);
                    \draw[gray] (1in,0) -- (2in,0) node[midway,above,black]{Tuesday} -- (2in,-15cm);
                    \draw[gray] (2in,0) -- (3in,0) node[midway,above,black]{Wednesday} -- (3in, -15cm);
                    \draw[gray] (3in,0) -- (4in,0) node[midway,above,black]{Thursday} -- (4in, -15cm);
                    \draw[gray] (4in,0) -- (5in,0) node[midway,above,black]{Friday} -- (5 in, -15cm);
                    
                    \path let % Course 1 on Mon
                        \p1 = (0,-1.5),
                        \p2 = (1in,-1.5),
                        \p3 = (0,{-1.5*(2+20/60)}),
                        \p4 = (1in,{-1.5*(2+20/60)}), in
                        coordinate (p1) at (\p1)
                        coordinate (p2) at (\p2)
                        coordinate (p3) at (\p3)
                        coordinate (p4) at (\p4)
                        coordinate (p5) at ($(\p1) !.5! (\p4)$);
                        \path[preaction={clip,postaction={fill=gPurple!20, draw=gPurple, line width=2pt}}] (p1) -- (p2) -- (p4) -- (p3) -- cycle;
                        \node at (p5) {\textbf{Course 1}};
                    
                    \path let % Course 1 on Wed
                        \p1 = (2in,-1.5),
                        \p2 = (3in,-1.5),
                        \p3 = (2in,{-1.5*(2+20/60)}),
                        \p4 = (3in,{-1.5*(2+20/60)}), in
                        coordinate (p1) at (\p1)
                        coordinate (p2) at (\p2)
                        coordinate (p3) at (\p3)
                        coordinate (p4) at (\p4)
                        coordinate (p5) at ($(\p1) !.5! (\p4)$);
                        \path[preaction={clip,postaction={fill=gPurple!20, draw=gPurple, line width=2pt}}] (p1) -- (p2) -- (p4) -- (p3) -- cycle;
                        \node at (p5) {\textbf{Course 1}};
                    
                    \path let % Course 1 on Fri
                        \p1 = (4in,-1.5),
                        \p2 = (5in,-1.5),
                        \p3 = (4in,{-1.5*(2+20/60)}),
                        \p4 = (5in,{-1.5*(2+20/60)}), in
                        coordinate (p1) at (\p1)
                        coordinate (p2) at (\p2)
                        coordinate (p3) at (\p3)
                        coordinate (p4) at (\p4)
                        coordinate (p5) at ($(\p1) !.5! (\p4)$);
                        \path[preaction={clip,postaction={fill=gPurple!20, draw=gPurple, line width=2pt}}] (p1) -- (p2) -- (p4) -- (p3) -- cycle;
                        \node at (p5) {\textbf{Course 1}};
                    
                    
                    
                    \path let % Course 2 on Mon
                        \p1 = (0,-1.5*3),
                        \p2 = (1in,-1.5*3),
                        \p3 = (0,{-1.5*(3+50/60)}),
                        \p4 = (1in,{-1.5*(3+50/60)}), in
                        coordinate (p1) at (\p1)
                        coordinate (p2) at (\p2)
                        coordinate (p3) at (\p3)
                        coordinate (p4) at (\p4)
                        coordinate (p5) at ($(\p1) !.5! (\p4)$);
                        \path[preaction={clip,postaction={fill=Pantone2347U!30, draw=Pantone2347U!80, line width=2pt}}] (p1) -- (p2) -- (p4) -- (p3) -- cycle;
                        \node at (p5) {\textbf{Course 2}};
                    
                    \path let % Course 2 on Wed
                        \p1 = (2in,-1.5*3),
                        \p2 = (3in,-1.5*3),
                        \p3 = (2in,{-1.5*(3+50/60)}),
                        \p4 = (3in,{-1.5*(3+50/60)}), in
                        coordinate (p1) at (\p1)
                        coordinate (p2) at (\p2)
                        coordinate (p3) at (\p3)
                        coordinate (p4) at (\p4)
                        coordinate (p5) at ($(\p1) !.5! (\p4)$);
                        \path[preaction={clip,postaction={fill=Pantone2347U!30, draw=Pantone2347U!80, line width=2pt}}] (p1) -- (p2) -- (p4) -- (p3) -- cycle;
                        \node at (p5) {\textbf{Course 2}};
                    
                    \path let % Course 2 on Fri
                        \p1 = (4in,-1.5*3),
                        \p2 = (5in,-1.5*3),
                        \p3 = (4in,{-1.5*(3+50/60)}),
                        \p4 = (5in,{-1.5*(3+50/60)}), in
                        coordinate (p1) at (\p1)
                        coordinate (p2) at (\p2)
                        coordinate (p3) at (\p3)
                        coordinate (p4) at (\p4)
                        coordinate (p5) at ($(\p1) !.5! (\p4)$);
                        \path[preaction={clip,postaction={fill=Pantone2347U!30, draw=Pantone2347U!80, line width=2pt}}] (p1) -- (p2) -- (p4) -- (p3) -- cycle;
                        \node at (p5) {\textbf{Course 2}};
                        
                        
                        
                    \path let % Course 3 on Mon
                        \p1 = (0,-1.5*7),
                        \p2 = (1in,-1.5*7),
                        \p3 = (0,{-1.5*(9+50/60)}),
                        \p4 = (1in,{-1.5*(9+50/60)}), in
                        coordinate (p1) at (\p1)
                        coordinate (p2) at (\p2)
                        coordinate (p3) at (\p3)
                        coordinate (p4) at (\p4)
                        coordinate (p5) at ($(\p1) !.5! (\p4)$);
                        \path[preaction={clip,postaction={fill=Pantone113U!20, draw=Pantone113U, line width=2pt}}] (p1) -- (p2) -- (p4) -- (p3) -- cycle;
                        \node at (p5) {\textbf{Course 3}};
                        
                        
                        
                    \path let % Course 4 on Tue
                        \p1 = (1in,-1.5*2),
                        \p2 = (2in,-1.5*2),
                        \p3 = (1in,{-1.5*(3+50/60)}),
                        \p4 = (2in,{-1.5*(3+50/60)}), in
                        coordinate (p1) at (\p1)
                        coordinate (p2) at (\p2)
                        coordinate (p3) at (\p3)
                        coordinate (p4) at (\p4)
                        coordinate (p5) at ($(\p1) !.5! (\p4)$);
                        \path[preaction={clip,postaction={fill=gEvergreen!20, draw=gEvergreen, line width=2pt}}] (p1) -- (p2) -- (p4) -- (p3) -- cycle;
                        \node at (p5) {\textbf{Course 4}};
                        
                    \path let % Course 4 on Fri
                        \p1 = (3in,-1.5*2),
                        \p2 = (4in,-1.5*2),
                        \p3 = (3in,{-1.5*(3+50/60)}),
                        \p4 = (4in,{-1.5*(3+50/60)}), in
                        coordinate (p1) at (\p1)
                        coordinate (p2) at (\p2)
                        coordinate (p3) at (\p3)
                        coordinate (p4) at (\p4)
                        coordinate (p5) at ($(\p1) !.5! (\p4)$);
                        \path[preaction={clip,postaction={fill=gEvergreen!20, draw=gEvergreen, line width=2pt}}] (p1) -- (p2) -- (p4) -- (p3) -- cycle;
                        \node at (p5) {\textbf{Course 4}};
                    \end{tikzpicture}
                \end{figure}
            
\end{document}

日程

答案1

这里有一个建议。安装您自己的坐标系统,其中 x ( hscale) 和 y ( vscale) 单位向量的长度分别为一定。实现“符号”坐标等MonTue例如Mon=0等。现在您可以使用坐标来引用时间表中的位置。然后定义一种样式,使调度成为可能。(我使用 24 小时时间,因为您可能希望创建一个在中午之前开始的时间表。)例如,

\node[course color=Pantone113U,course=Mon from 19 to 22]{Course 3};

Pantone113U将在周一晚上 7 点至 10 点安排一门课程,并使用课程的颜色和名称Course 3。对于在不同日期重复的课程,您可以使用foreach。然后使用以下方法实现示例的整个课程安排

 \path 
  foreach \X in {Mon,Wed,Fri} 
  {node[course={\X} from 13 to 14.25]{Course 1}
  node[course color=Pantone2347U,course={\X} from 15 to 16]{Course 2}}
  node[course color=Pantone113U,course=Mon from 19 to 22]{Course 3}
  foreach \X in {Tue,Thu}
  {node[course color=gEvergreen,course={\X} from 14 to 15]{Course 4}} ;

完整代码和结果:

\documentclass[tikz,border=3mm]{standalone}
\definecolor{Pantone113U}{RGB}{255 225 94}
\definecolor{Pantone2347U}{RGB}{231 69 54}
\definecolor{gPurple}{RGB}{117 102 160}
\definecolor{gEvergreen}{RGB}{0 78 66}
\begin{document}
\begin{tikzpicture}[font=\sffamily,
    declare function={Mon=0;Tue=1;Wed=2;Thu=3;Fri=4;
    hscale=2.5;vscale=1.2;tmin=12;tmax=22;},
    yscale=-1,x=hscale*1cm,y=vscale*1cm,
    course color/.code={\colorlet{coursecolor}{#1}},
    course color=gPurple,
    course/.style args={#1 from #2 to #3}{%
            draw=coursecolor,fill=coursecolor!20,
            at={(#1,#2)},align=center,
            node font=\bfseries,
            anchor=north west,outer sep=0pt,
            minimum width=hscale*1cm,
            minimum height={abs(#3-#2)*vscale*1cm}}]
 \pgfmathtruncatemacro{\tmin}{tmin}%
 \pgfmathtruncatemacro{\tnext}{tmin+1}%
 \pgfmathtruncatemacro{\tmax}{tmax} 
 \draw[xstep=hscale*1cm,ystep=vscale*1cm]  (0,tmin) grid (5,tmax);
 \path foreach \X  [count=\Y] in
    {Monday,Tuesday,Wednesday,Thursday,Friday}
    {(\Y-0.5,12) node[above,text depth=0.25ex]{\X} }
     foreach \X [parse=true] in {\tmin,\tnext,...,\tmax}
    {(0,\X) node[left]{\ifnum\X<12\relax
     \X\ a.m.%
    \else
     \ifnum\X=12\relax
      12\ p.m.%
     \else
      \the\numexpr\X-12\relax\ p.m.%
     \fi
    \fi} }; 
 \path 
  foreach \X in {Mon,Wed,Fri} 
  {node[course={\X} from 13 to 14.25]{Course 1}
  node[course color=Pantone2347U,course={\X} from 15 to 16]{Course 2}}
  node[course color=Pantone113U,course=Mon from 19 to 22]{Course 3}
  foreach \X in {Tue,Thu}
  {node[course color=gEvergreen,course={\X} from 14 to 15]{Course 4}} ;
\end{tikzpicture}
\end{document}

在此处输入图片描述

相关内容