
我希望產生多個部分,每個部分後面跟著多行 i。這就是我使用 multido 重複自訂指令 \drawline i 次的方法。
\documentclass[12pt]{memoir}
\usepackage{tikz}
\usepackage{multido}
\newcommand{\drawline}{%
\begin{tikzpicture}%
\draw [gray] (0,0) -- (\textwidth, 0);%
\node [above right] at (0,0) {\i.};
\end{tikzpicture}%
\\%
}
\begin{document}
\section*{Section 1}
\multido{\i=1+1}{5}{\drawline}
\section*{Section 2}
\multido{\i=1+1}{4}{\drawline}
\end{document}
產生這個:
\\
我遇到的問題似乎與tikzpicture 定義後面的斷線有關。我需要在 \drawline 呼叫之間斷行,以便這些行按順序顯示,而不是彼此相鄰。但是,我有兩個問題:
- 在最後一次呼叫 \drawline 之後,我得到了一個額外的不必要的空行,這意味著部分之間有額外的空間。
- 當一個部分的行或多或少完美地填充一頁時,下一部分將出現在下一頁上,但不會出現在頁面頂部。頁面頂部和章節標題之間有空格。
根據文檔,multido 的語法是這樣的:
\multido{*variables*}{*repetitions*}{*stuff*}
有沒有辦法可以參考重複作為變數?例如,如果希望換行符號適用於所有 n,其中 n <重複?在這種情況下,除了最後一種情況之外,可以使用條件在所有情況下換行。
或者,可以使用 tikzpicture 中相對於另一 tikzpicture 的座標嗎?這樣 \drawline 就可以被定義而無需求助於\\
最後的。
答案1
載入multido
時不需要使用。tikz
只需使用\foreach
:
\documentclass[12pt]{memoir}
\usepackage{tikz}
\newcommand{\linespace}{6mm}
\newcommand{\drawlines}[1]{\tikz{\foreach \n in {1,...,#1}
{\draw[gray](0,-\n*\linespace)node[above right,black]{\n.}--(\textwidth,-\n*\linespace);}}}
\begin{document}
\section*{Section 1}
\drawlines{5}
\section*{Section 2}
\drawlines{4}
\end{document}
答案2
一種相當通用的方法,用於重複給定次數的事情,也使用相應的索引。
\documentclass[12pt]{memoir}
\ExplSyntaxOn
\NewDocumentCommand{\REPEAT}{mm}
{% #1 = number of lines to print, #2 = what to repeat
\int_step_inline:nn { #1 } { #2 }
}
\ExplSyntaxOff
\newcommand{\drawline}[1]{%
\noindent\makebox[0pt][l]{#1.}%
\leaders\hrule height -2pt depth 2.2pt \hfill
\hspace*{0pt}\par
}
\newcommand{\drawlines}[1]{\REPEAT{#1}{\drawline{##1}}}
\begin{document}
\section{Title}
\drawlines{5}
\section{Title}
\drawlines{4}
\end{document}
第一個參數\REPEAT
是重複次數;第二個參數是要執行的程式碼,我們可以透過 引用索引##1
。
在特定情況下,您需要\REPEAT
宏\drawline
,它將當前索引設定在向右突出的零寬度框中,然後繪製全文寬度的規則,低於基線 2pt,厚度為 0.2pt。
答案3
沒有必要使用Tik為此,您還可以使用 LaTeX 的內建功能\rule
:
\documentclass[12pt]{memoir}
\usepackage{xcolor}
\makeatletter
\newcommand\drawline[1]
{\expandafter\drawline@\expandafter1\expandafter;\the\numexpr#1;}
\def\drawline@#1;#2;%
{%
\ifnum#1>#2
\expandafter\@gobbletwo
\fi
\@firstofone
{%
\drawline@output{#1}%
\expandafter\drawline@\the\numexpr#1+1;#2;%
}%
}
\newcommand\drawline@output[1]
{%
\par
\noindent\rlap{#1.}%
\textcolor{gray}{\rule[-0.3333em]{\linewidth}{.4pt}}%
\par
}
\makeatother
\begin{document}
\section*{Section 1}
\drawline{5}
\section*{Section 2}
\drawline{4}
\end{document}
答案4
使用的 LaTeX3 版本\rule
(基於 Skillmon 的答案)
\documentclass{article}
\usepackage{xcolor}
\ExplSyntaxOn
\int_new:N \g_dl_line_count
\NewDocumentCommand{\drawlines}{ m }{
\int_set:Nn \g_dl_line_count {1}
\prg_replicate:nn {#1} {%
\par
\noindent\rlap{\int_use:N \g_dl_line_count.}%
\textcolor{gray}{\rule[-0.3333em]{\linewidth}{.4pt}}%
\par
\int_incr:N \g_dl_line_count
}
}
\ExplSyntaxOff
\begin{document}
\section*{Section 1}
\drawlines{5}
\section*{Section 2}
\drawlines{4}
\end{document}