使用对齐的子内容格式化列

使用对齐的子内容格式化列

这里是我的文档的一个片段。

\section{Events}

\begin{multicols}{2}
    \subsection{Monday}
    \textit{10:15am–11:15am}\dotfill\ Panel 2\smallskip\\
    {Text here,\\
    text there}

    \vfill\columnbreak

    \subsection{Wednesday}
    \textit{9:00am–10:00am}\dotfill\ Panel 1\smallskip\\
    {Text over there}
    \newline % 3rd-line padding
\end{multicols}
\begin{multicols}{2}
    \noindent\textit{12:45pm–1:45pm}\dotfill\ Panel 1\smallskip\\
    {Text down here}

    \vfill\columnbreak

    \noindent\textit{11:30am–12:30pm}\dotfill\ Panel 2\smallskip\\
    {Text everywhere}
\end{multicols}

它看起来是这样的。它或多或少就是我想要的样子。

在此处输入图片描述

问题是这些列会无限期地延续,并且可能会移动和替换条目。显然,按照我上面构建的方式,任何这样的小调整都是不切实际的。还有一个额外的不便之处是,当一对条目的长度不同时,我必须填充较短的条目,否则就会出现这样的混乱。

在此处输入图片描述

我的问题是:我怎样才能做得更好?

答案1

这只是初步解决方案——但我不建议使用 8 个参数。稍后我将尝试使用键值语法进行更新。

\documentclass{article}


\usepackage{etoolbox}
\usepackage{longtable}

\DeclareRobustCommand{\timedisplayline}[2]{%
  \textit{#1} \dotfill\ #2 
}

\DeclareRobustCommand{\tableline}[8]{%
  \protect\subsection{#1} & \protect\subsection{#4}  \tabularnewline
  \timedisplayline{#2}{#3} &   \timedisplayline{#5}{#6} \tabularnewline
  \ifblank{#7}{}{#7} &   \ifblank{#8}{}{#8} \tabularnewline
}

\begin{document}


\section{Events}

\begin{longtable}{*{2}{p{0.4\textwidth}}}
  \tableline{Monday}{10:15am-11:15am}{Panel 2}{Wednesday}{9:00am-10:00am}{Panel 1}{Some text}{Other text}
  \tableline{Monday}{10:15am-11:15am}{Panel 2}{Wednesday}{9:00am-10:00am}{Panel 1}{}{} 
  \tableline{Monday}{10:15am-11:15am}{Panel 2}{Wednesday}{}{Panel 4}{}{}
\end{longtable}

\end{document}

在此处输入图片描述

更新更好的解决方案

\lefttitle我添加了名为、lefttime和的键值leftpanel及其等价物。它们仅在不为空时才显示。单元格的分组避免了明确清除它们的需要,但它们最初都是空的。leftdescription\right...

这样,就不需要记住哪个参数对应哪个条目。

最好的办法是对左表和右表使用两个单独的命令。

\documentclass{article}


\usepackage{etoolbox}
\usepackage{longtable}
\usepackage{xstring}
\usepackage{xkeyval}

\makeatletter
\define@key{panelstuff}{lefttitle}[]{%
  \def\KVlefttitle{#1}%
}
\define@key{panelstuff}{righttitle}[]{%
  \gdef\KVrighttitle{#1}%
}

\define@key{panelstuff}{lefttime}[]{%
  \gdef\KVlefttime{#1}%
}

\define@key{panelstuff}{righttime}[]{%
  \gdef\KVrighttime{#1}%
}

\define@key{panelstuff}{leftpanel}[]{%
\gdef\KVleftpanel{#1}% 
}

\define@key{panelstuff}{rightpanel}[]{%
  \gdef\KVrightpanel{#1} 
}



\define@key{panelstuff}{leftdescription}[]{%
\gdef\KVleftdescription{#1}%
}

\define@key{panelstuff}{rightdescription}[]{%
\gdef\KVrightdescription{#1}%
}



\newcommand{\displaydescription}[1]{%
  \StrLen{#1}[\@tempa]%
  \ifnum\@tempa > 0 
  #1%
  \fi%
}

\newcommand{\displaytitle}[1]{%
  \StrLen{#1}[\@tempa]%
  \ifnum\@tempa > 0 
  \protect\subsection{#1}%
  \fi%
}

\DeclareRobustCommand{\timedisplayline}[2]{%
  \fullexpandarg
  \StrLen{#1}[\@tempa]%
  \StrLen{#2}[\@tempb]%

  \ifnum\@tempa > 0 
  \textit{#1}\fi
  \ifnum \numexpr\@tempa + \@tempb > 0 % Only show a dotted line if at least one of the entries is available
  \dotfill\ %  
  \fi
  \ifnum\@tempb > 0 
  #2%
  \fi%
}

\makeatother


\presetkeys{panelstuff}{}{%
  lefttitle={},
  righttitle={},
  lefttime={},
  righttime={},
  leftpanel={},
  rightpanel={},
  leftdescription={},
  rightdescription={}
}


\DeclareRobustCommand{\tableline}[1]{%
  \setkeys{panelstuff}{#1}%
  \displaytitle{\KVlefttitle} &   \displaytitle{\KVrighttitle} \tabularnewline
  \timedisplayline{\KVlefttime}{\KVleftpanel} &   \timedisplayline{\KVrighttime}{\KVrightpanel} \tabularnewline
  \displaydescription{\KVleftdescription} &   \displaydescription{\KVrightdescription} \tabularnewline
}

\begin{document}

\section{Events}

\begin{longtable}{*{2}{p{0.4\textwidth}}}
  \tableline{lefttitle=Monday,lefttime={10:15am-11:15am},leftpanel={Panel 2},
    righttitle={Wednesday},righttime={9:00am-10:00am},rightpanel={Panel 1},leftdescription={Some text},rightdescription={Other text}}
  \tableline{lefttitle=Tuesday,lefttime={10:15am-11:15am},leftpanel={Panel 2},
    righttitle={Wednesday},righttime={9:00am-10:00am},rightpanel={Panel 4}}

  \tableline{lefttitle=Tuesday,leftpanel={Panel 2},
    righttitle={Wednesday},righttime={9:00am-10:00am},rightpanel={Panel 1}}

  \tableline{leftpanel={Panel 17},
    righttitle={Thursday},righttime={9:00am-10:00am},rightpanel={Panel 1}}

  \tableline{righttitle={Thursday},righttime={9:00am-10:00am},rightpanel={Panel 1}}

\end{longtable}

\end{document}

在此处输入图片描述


另一个更新版本,可能具有格式更改和一些可配置性

\documentclass{article}

\usepackage{longtable}
\usepackage[x11names,svgnames]{xcolor}
\usepackage{xstring}
\usepackage{xkeyval}

\makeatletter


\newif\ifusedotfill
\usedotfillfalse


\newif\ifdisplayifempty
\displayifemptytrue


\newcommand{\displaydefaultpanel}{%
  n.y.d.% not yet determined 
}

\newcommand{\displaydefaultdescription}{%
  N.A.% Not Available
}


\newcommand{\displaydefaulttime}{%
  t.b.a% to be announced
}

\newcommand{\displaydefaulttitle}{%
  N.Y.D.% Not Yet Determined
}


% Define both a \left... and a \right... key 
\newcommand{\definekeypair}[1]{%
  \define@key{panelstuff}{left#1}[]{%
    \expandafter\gdef\csname KVleft#1\endcsname{##1}%
  }%
  \define@key{panelstuff}{right#1}[]{%
    \expandafter\gdef\csname KVright#1\endcsname{##1}%
  }%
}

\define@key{panelposition}{position}[left]{%
  \gdef\KVposition{#1}%
}


\newcommand{\definekeypairformat}[1]{%
\definekeypair{#1}%
% Define the format key pair too
\definekeypair{#1format}%
}

\definekeypairformat{title}
\definekeypairformat{time}
\definekeypairformat{starttime}
\definekeypairformat{endtime}
\definekeypairformat{description}
\definekeypairformat{panel}


% Generic formats for the individual parts

\newcommand{\GenericDescriptionFormat}[1]{%
  \textnormal{#1}%
}

\newcommand{\GenericTitleFormat}[1]{%
  \protect\subsection{#1}%
}

\newcommand{\GenericTimeFormat}[1]{%
  \textcolor{blue}{\textit{#1}}%
}

\newcommand{\GenericPanelFormat}[1]{%
 \textsc{#1}%
}

%%%% Individual formats for the right and left content

\newcommand{\LeftDescriptionFormat}[1]{%
  \textcolor{red}{\textbf{#1}}%
}

\newcommand{\RightDescriptionFormat}[1]{%
  \textcolor{brown}{\textnormal{#1}}%
}

\newcommand{\RightTimeFormat}[1]{%
  \textcolor{blue}{\textit{#1}}%
}

\newcommand{\LeftTimeFormat}[1]{%
  \textcolor{DarkGreen}{\textit{#1}}%
}


\newcommand{\LeftPanelFormat}[1]{%
 \textsc{#1}%
}

\newcommand{\RightPanelFormat}[1]{%
 \sffamily {#1}\normalfont%
}


\newcommand{\LeftTitleFormat}[1]{%
  \protect\subsection*{#1}%
}

\newcommand{\RightTitleFormat}[1]{%
  \protect\subsection*{\textcolor{Green}{#1}}%
}


%%%%

% 'Generic' display format 
\newcommand{\@@showwithlocalformat}[3][position=left]{%
  \setkeys{panelposition}{#1}%
  \expandafter\csname KV\KVposition #2format\endcsname{#3}%
}%


% 
\newcommand{\@@decidewhattoshow}[3]{%
  \ifnum #1 > 0%
  \xdef\@@stringtoshow{#2}% preset with some value.
  \else\ifdisplayifempty\xdef\@@stringtoshow{#3}\fi%
  \fi%
}



% Display the description

\newcommand{\displaydescription}[2][position=left]{%
  \xdef\@@stringtoshow{}%
  \fullexpandarg%
  \StrLen{#2}[\@tempa]%
  \@@decidewhattoshow{\@tempa}{#2}{\displaydefaultdescription}%
  \@@showwithlocalformat[#1]{description}{\@@stringtoshow}%
  \xdef\@@stringtoshow{}%
}

% Display the title

\newcommand{\displaytitle}[2][position=left]{%
  \xdef\@@stringtoshow{}%
  \fullexpandarg%
  \StrLen{#2}[\@tempa]%
  \@@decidewhattoshow{\@tempa}{#2}{\displaydefaulttitle}%
  \@@showwithlocalformat[#1]{title}{\@@stringtoshow}%
  \xdef\@@stringtoshow{}%
}

% Display the time




\newcommand{\displaytime}[3][position=left]{%
  \xdef\@@stringtoshow{}%
  \fullexpandarg%
  \StrLen{#2}[\@tempa]%
  \StrLen{#3}[\@tempb]%
  \@@decidewhattoshow{\@tempa}{#2}{\displaydefaulttime}%
  \@@showwithlocalformat[#1]{time}{\@@stringtoshow}%
  \ifusedotfill%
  \dotfill\ %
  \else%
  \hfill\ % No dots here...
  \fi%
  \@@decidewhattoshow{\@tempb}{#3}{\displaydefaultpanel}%
  \@@showwithlocalformat[#1]{panel}{\@@stringtoshow}%
  \xdef\@@stringtoshow{} % 'clearing' the definition
}


\makeatother


\presetkeys{panelstuff}{}{%
  leftdescriptionformat=\LeftDescriptionFormat,
  rightdescriptionformat=\RightDescriptionFormat,
  lefttimeformat  = \LeftTimeFormat,
  righttimeformat = \RightTimeFormat,
  leftpanelformat  = \LeftPanelFormat,
  rightpanelformat = \RightPanelFormat,
  lefttitleformat  = \LeftTitleFormat,
  righttitleformat = \RightTitleFormat,
  lefttitle={},
  righttitle={},
  lefttime={},
  righttime={},
  leftpanel={},
  rightpanel={},
  leftdescription={},
  rightdescription={}
}


\DeclareRobustCommand{\tableline}[1]{%
  \setkeys{panelstuff}{#1}%
  \displaytitle{\KVlefttitle}               &   \displaytitle[position=right]{\KVrighttitle}              \tabularnewline
  \displaytime{\KVlefttime}{\KVleftpanel}   &   \displaytime[position=right]{\KVrighttime}{\KVrightpanel} \tabularnewline
  \displaydescription{\KVleftdescription}   &   \displaydescription[position=right]{\KVrightdescription}  \tabularnewline
}

\begin{document}

\section{Events}

\begin{longtable}{*{2}{p{0.4\textwidth}}}
  \tableline{lefttitle=Monday,lefttime={10:15am-11:15am},leftpanel={Panel 2},
    righttitle={Wednesday},righttime={9:00am-10:00am},rightpanel={Panel 1},leftdescription={Some text},rightdescription={Other text}}
  \tableline{leftdescriptionformat=\GenericDescriptionFormat,lefttitle=Tuesday,lefttime={10:15am-11:15am},leftpanel={Panel 2},    righttitle={Wednesday},righttime={9:00am-10:00am},rightpanel={Panel 4}, leftdescription={And now for something completely different!}}
  % Now use an example, where the right time format is the same as the left one
  \tableline{lefttitle=Tuesday,leftpanel={Panel 2}, righttitle={Wednesday},
    righttime={9:00am-10:00am}, rightpanel={Panel 1},righttimeformat=\LeftTimeFormat}
  \tableline{leftpanel={Panel 17},
    righttitle={Thursday},righttime={9:00am-10:00am},rightpanel={Panel 1}}
  % Now use the Generic title format for the right title
  \tableline{righttitle={Thursday},righttime={9:00am-10:00am},rightpanel={Panel 1},
    righttitleformat=\GenericTitleFormat,leftdescription={One ring to rule them all}}
\end{longtable}

\end{document}

在此处输入图片描述

相关内容