LaTeX で任意の数の線を描くための最良の方法は何ですか?

LaTeX で任意の数の線を描くための最良の方法は何ですか?

複数のセクションを作成し、各セクションの後に複数の線 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 の呼び出し間で線を改行して、線が隣り合わず連続して表示されるようにする必要があります。ただし、次の 2 つの問題があります。

  1. \drawline の最後の呼び出しの後に、不要な空行が追加され、セクション間に余分なスペースができます。
  2. 1 つのセクションの行がほぼ完全にページを埋め尽くすと、次のセクションはページの上部ではなく次のページに表示されます。ページの上部とセクション タイトルの間には空白があります。

ドキュメントによると、multido の構文は次のとおりです。

\multido{*variables*}{*repetitions*}{*stuff*}

参照する方法はありますか?繰り返し変数として?例えば、n < 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は繰り返し回数です。2 番目の引数は実行するコードで、インデックスは で参照できます##1

特定のケースでは、現在のインデックスを右に突き出た幅ゼロのボックスに設定し、ベースラインの 2 ポイント下に 0.2 ポイントの太さでテキスト全体の幅の罫線を描画するマクロが\REPEAT必要です。\drawline

ここに画像の説明を入力してください

答え3

Tiを使用する必要はありませんZ の場合、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}

ここに画像の説明を入力してください

関連情報