如何用 \twocolumn 制作垂直虚线?

如何用 \twocolumn 制作垂直虚线?

我的代码在两列之间创建了一条垂直线,但我不确定如何使它变成虚线。

\usepackage[automark]{scrlayer-scrpage}
\usepackage[english]{babel}
\usepackage{blindtext}
\PassOptionsToPackage{svgnames}{xcolor}
\usepackage{tikz}
\usepackage{xcolor}
\usepackage{fancyhdr}
\usepackage{calc}
\usepackage[width = 18cm]{geometry}
\usepackage{enumitem}
\newcounter{question}
\newcounter{choice}
\setcounter{choice}{1}

\newcommand{\tikzhead}[1]{%
  \begin{tikzpicture}[remember picture,overlay]
    \node[yshift=-2.7cm, xshift=-1cm] at (current page.north west)
      {\begin{tikzpicture}[remember picture, overlay]
        \path[draw=none, fill=lightgray] (2,0) rectangle
          (\paperwidth,2cm);
       \end{tikzpicture}
      };
   \end{tikzpicture}}
\clearscrheadings
\ihead{\tikzhead{\headmark}}
\pagestyle{scrheadings}

\newenvironment{question}{%
  \stepcounter{question}%
  \noindent\colorbox{black}{\textcolor{white}{\makebox[1.5em][c]{\thequestion}}}%
\colorbox{lightgray}{\phantom{\rule{\columnwidth-1.9em-\columnsep}{\heightof{1}}}}\par
  \begin{list}{}{%
    \setlength\itemindent{0pt}%
    \setlength\leftmargin{2em}%
    \setlength\rightmargin{1em}%
    \setlength\labelwidth{0pt}%
    \setlength\labelsep{0pt}}%
    \item[]}%
{\end{list}}

\newenvironment{choices}{%
  \begin{list}{\Alph{choice})}{%
    \setlength\labelwidth{30pt}%
    \setlength\itemindent{0pt}%
    \setlength\leftmargin{2em}%
    \setlength\labelsep{10pt}%
    \usecounter{choice}}}%
  {\end{list}}
\setlength\columnseprule{0.5pt}


\begin{document}
\pagenumbering{gobble}



\twocolumn
\begin{question}
  What is 1 + 2?
\vspace{1.5cm}
\end{question}

\begin{question}
  What is 1 + 3?
\vspace{1.5cm}
\end{question}

\begin{question}
  What is 1 + 4?
  \begin{choices}
    \item 1
    \item 2
    \item 5
    \item 6
  \end{choices}
  \vspace{0.5cm}
\end{question}

\begin{question}
  What is 1 + 2?
\vspace{1.5cm}
\end{question}

\begin{question}
  What is 1 + 3?
\vspace{1.5cm}
\end{question}

\vspace{5cm}

\begin{question}
  What is 1 + 4?
  \begin{choices}
    \item 1
    \item 2
    \item 5
    \item 6
  \end{choices}
  \vspace{0.5cm}
\end{question}

\begin{question}
  What is 1 + 2?
\vspace{1.5cm}
\end{question}

\begin{question}
  What is 1 + 3?
\vspace{1.5cm}
\end{question}

\begin{question}
  What is 1 + 4?
  \begin{choices}
    \item 1
    \item 2
    \item 5
    \item 6
  \end{choices}
  \vspace{0.5cm}
\end{question}

\begin{question}
  What is 1 + 2?
\vspace{1.5cm}
\end{question}

\end{document}```

答案1

这使用\AddToHooktikzpagenodes。

\documentclass[twocolumn]{report}
\usepackage[automark]{scrlayer-scrpage}
\usepackage[english]{babel}
\usepackage{blindtext}
\PassOptionsToPackage{svgnames}{xcolor}
\usepackage{tikzpagenodes}% also loads tikz
\usepackage{xcolor}
%\usepackage{fancyhdr}
\usepackage{calc}
\usepackage[width = 18cm, headheight=2cm]{geometry}
\usepackage{enumitem}
\newcounter{question}
\newcounter{choice}
\setcounter{choice}{1}

\newsavebox{\headbox}
\savebox{\headbox}{\begin{tikzpicture}
  \path[draw=none, fill=lightgray] (2,0) rectangle (\paperwidth,2cm);
\end{tikzpicture}}
%\geometry{headheight=\ht\headbox}

\pagestyle{scrheadings}% ???

\newenvironment{question}{%
  \stepcounter{question}%
  \noindent\colorbox{black}{\textcolor{white}{\makebox[1.5em][c]{\thequestion}}}%
\colorbox{lightgray}{\phantom{\rule{\columnwidth-1.9em-\columnsep}{\heightof{1}}}}\par
  \begin{list}{}{%
    \setlength\itemindent{0pt}%
    \setlength\leftmargin{2em}%
    \setlength\rightmargin{1em}%
    \setlength\labelwidth{0pt}%
    \setlength\labelsep{0pt}}%
    \item[]}%
{\end{list}}

\newenvironment{choices}{%
  \begin{list}{\Alph{choice})}{%
    \setlength\labelwidth{30pt}%
    \setlength\itemindent{0pt}%
    \setlength\leftmargin{2em}%
    \setlength\labelsep{10pt}%
    \usecounter{choice}}}%
  {\end{list}}
%\setlength\columnseprule{0.5pt}

\AddToHook{shipout/background}{\begin{tikzpicture}[overlay,remember picture]
  \node at (current page header area.center) {\usebox\headbox};
  \draw[dotted] (current page text area.north) -- (current page text area.south);
\end{tikzpicture}}


\begin{document}
\pagenumbering{gobble}



\twocolumn
\begin{question}
  What is 1 + 2?
\vspace{1.5cm}
\end{question}

\begin{question}
  What is 1 + 3?
\vspace{1.5cm}
\end{question}

\begin{question}
  What is 1 + 4?
  \begin{choices}
    \item 1
    \item 2
    \item 5
    \item 6
  \end{choices}
  \vspace{0.5cm}
\end{question}

\begin{question}
  What is 1 + 2?
\vspace{1.5cm}
\end{question}

\begin{question}
  What is 1 + 3?
\vspace{1.5cm}
\end{question}

\vspace{5cm}

\begin{question}
  What is 1 + 4?
  \begin{choices}
    \item 1
    \item 2
    \item 5
    \item 6
  \end{choices}
  \vspace{0.5cm}
\end{question}

\begin{question}
  What is 1 + 2?
\vspace{1.5cm}
\end{question}

\begin{question}
  What is 1 + 3?
\vspace{1.5cm}
\end{question}

\begin{question}
  What is 1 + 4?
  \begin{choices}
    \item 1
    \item 2
    \item 5
    \item 6
  \end{choices}
  \vspace{0.5cm}
\end{question}

\begin{question}
  What is 1 + 2?
\vspace{1.5cm}
\end{question}

\end{document}

答案2

您的帖子中有很多可疑的代码,而我只做了很少的清理工作。关于您的实际问题,最简单的方法可能是将列分隔符设置为0pt宽度,然后添加带有 的虚线tikz,因为无论如何您都会使用它。

我做了以下假设:

  • 您正在使用scrarticle(因为您正在加载 Koma 包);
  • 您正在使用 A4 纸(同上);
  • 尽管如此,您还是需要美式连字符模式(因为除非您的系统配置不同,否则您明确加载了它);
  • scrlayer-scrpage对你来说比更重要fancyhdr
  • geometry考虑到其他所有情况,你可能想要也可能不想要;
  • enumitem由于您在这里不使用它,所以没有其他加载的理由;
  • 由于某种原因,您没有用填充文本填充您的文档并且不需要blindtext

然后,以下生成一个虚线规则作为列分隔符,编译时不会出现错误,并且避免嵌套tikzpicture

\documentclass[american,a4paper]{scrarticle}% or something?
\usepackage[automark]{scrlayer-scrpage}
\usepackage[english]{babel}% american, presumably
% \usepackage{blindtext} % unused, unnecessary
\PassOptionsToPackage{svgnames}{xcolor}
\usepackage{tikz}
\usepackage{tikzpagenodes}
% \usepackage{xcolor} % loaded by tikz
% \usepackage{fancyhdr} % incompatible with scrlayer-scrpage
\usepackage{calc}
\usepackage[width = 18cm]{geometry}% shouldn't be used with scrlayer-scrpage/koma classes if at all possible 
% \usepackage{enumitem} % unused
\newcounter{question}
\newcounter{choice}
\setcounter{choice}{1}

\newcommand{\tikzhead}[1]{% avoid nesting tikzpictures
  \begin{tikzpicture}[remember picture,overlay]
    \path[draw=none, fill=lightgray] ([yshift=-2.7cm, xshift=1cm]current page.north west) coordinate (lr) rectangle ++({\paperwidth-2cm},2cm);
    \draw [dashed] (current page text area.north) -- (current page text area.south);
  \end{tikzpicture}}
\clearmainofpairofpagestyles % replace deprecated macro
\ihead{\tikzhead{\headmark}}
\pagestyle{scrheadings}

\newenvironment{question}{%
  \stepcounter{question}%
  \noindent\colorbox{black}{\textcolor{white}{\makebox[1.5em][c]{\thequestion}}}%
  \colorbox{lightgray}{\phantom{\rule{\columnwidth-1.9em-\columnsep}{\heightof{1}}}}\par
\begin{list}{}{%
    \setlength\itemindent{0pt}%
    \setlength\leftmargin{2em}%
    \setlength\rightmargin{1em}%
    \setlength\labelwidth{0pt}%
    \setlength\labelsep{0pt}}%
  \item[]}%
{\end{list}}

\newenvironment{choices}{%
  \begin{list}{\Alph{choice})}{%
      \setlength\labelwidth{30pt}%
      \setlength\itemindent{0pt}%
      \setlength\leftmargin{2em}%
      \setlength\labelsep{10pt}%
      \usecounter{choice}}}%
  {\end{list}}
\setlength\columnseprule{0.5pt}


\begin{document}
\pagenumbering{gobble}

\setlength{\columnseprule}{0pt}

\twocolumn
\begin{question}
  What is 1 + 2?
\vspace{1.5cm}
\end{question}

\begin{question}
  What is 1 + 3?
\vspace{1.5cm}
\end{question}

\begin{question}
  What is 1 + 4?
  \begin{choices}
    \item 1
    \item 2
    \item 5
    \item 6
  \end{choices}
  \vspace{0.5cm}
\end{question}

\begin{question}
  What is 1 + 2?
\vspace{1.5cm}
\end{question}

\begin{question}
  What is 1 + 3?
\vspace{1.5cm}
\end{question}

\vspace{5cm}

\begin{question}
  What is 1 + 4?
  \begin{choices}
    \item 1
    \item 2
    \item 5
    \item 6
  \end{choices}
  \vspace{0.5cm}
\end{question}

\begin{question}
  What is 1 + 2?
\vspace{1.5cm}
\end{question}

\begin{question}
  What is 1 + 3?
\vspace{1.5cm}
\end{question}

\begin{question}
  What is 1 + 4?
  \begin{choices}
    \item 1
    \item 2
    \item 5
    \item 6
  \end{choices}
  \vspace{0.5cm}
\end{question}

\begin{question}
  What is 1 + 2?
\vspace{1.5cm}
\end{question}

\end{document}

注意

  • enumitem如果你使用它,将使你的生活变得更轻松;
  • 插入明确的间距应该是最后的手段,而且绝对不会经常发生;
  • 插入大量固定空间往往会导致分页效果不佳;
  • 我目前无法制作出毫无价值的输出图像。

[我尝试制作一张图片,但甚至没有显示出那条该死的线 --- 更不用说它现在是虚线了。]

相关内容