セクションのスタイルを制御して各セクションの前に水平線を追加する方法

セクションのスタイルを制御して各セクションの前に水平線を追加する方法

このように、各セクションの前に水平線を追加したいです

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

手動で各セクションの前に置くこともできます\rule{\textwidth}{1pt}が、これを実現するには、たとえばセクションのスタイルを制御するいくつかのコードをプリアンブルに置くなど、より柔軟な方法が必要です。

答え1

おそらく、最初のセクションの前や、セクションがページの先頭で始まるときにルールを適用したくないでしょう。次のコードでこれを実現します。

\documentclass{article}

\usepackage[T1]{fontenc}
\usepackage[utf8]{inputenc}
\usepackage{titlesec}

\usepackage{lipsum}

\titleformat{\section}
  {\sectionrule\Large\bfseries}
  {\thesection}
  {1em}
  {}

% this command is executed at each \section command
\newcommand{\sectionrule}{%
  % no rule before the first section
  \ifnum\value{section}=0
  \else
    % otherwise, ensure being between paragraphs
    \par
    % add some vertical space
    \addvspace{\bigskipamount}%
    % the rule realized as leaders, so it disappears at a page break
    % see also http://tex.stackexchange.com/a/61643/4427
    \leaders\vrule width \textwidth\vskip0.4pt
    % some other vertical space
    \bigskip
  \fi
}


\begin{document}

\section{First}

\lipsum[1-3]

\section{Second}

\lipsum[2-6]\lipsum[2]\lipsum[2]

\section{Third}

\lipsum

\end{document} 

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

代替の解決策として、ページ区切りの場合にページの上部に罫線を配置します。

\documentclass{article}

\usepackage[T1]{fontenc}
\usepackage[utf8]{inputenc}
\usepackage{titlesec}

\usepackage{lipsum}

\titleformat{\section}
  {\sectionrule\Large\bfseries}
  {\thesection}
  {1em}
  {}

\newcommand{\sectionrule}{%
    \par
    \addvspace{\bigskipamount}%
    \hrule
    \nopagebreak
    \bigskip
}

\begin{document}

\section{First}

\lipsum[1-3]

\section{Second}

\lipsum[2-6]\lipsum[2]\lipsum[2]

\section{Third}

\lipsum

\end{document}

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

答え2

titlesec を使えば簡単です:

\documentclass{article}% http://ctan.org/pkg/amsproc

\usepackage[utf8]{inputenc}
\usepackage[T1]{fontenc}
\usepackage{fourier}
\usepackage{titlesec}

\titleformat{\section}{\vbox{\rule{\linewidth}{0.8pt}}\bigskip\LARGE\bfseries}{\thesection}{1em}{}
\begin{document}
\begin{equation}
  v = \sqrt{2gl(\cos\phi_0 - \cos \phi)}
\end{equation}
\section{Section two}

\end{document} 

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

関連情報