根据左列或右列的位置定义自定义部分命令

根据左列或右列的位置定义自定义部分命令

我正在尝试使用\sectiontitlesec包定制命令,以便结果会根据节标题位于左列还是右列而有所不同。

理想情况下,我想要一些类似于下面的 MWE 的东西,但是其中leftsectionrightsection命令组合成一个命令,确定当前标题是在左列还是右列。

% !TeX program = lualatex
\documentclass[english,a4paper,10pt,twocolumn]{article}

\usepackage[english]{babel}
\usepackage[T1]{fontenc} 
\usepackage[explicit]{titlesec}
\usepackage{lipsum}

% Left section
\titleclass{\leftsection}{straight}[\section]
\newcounter{leftsection}
\titleformat{\leftsection}{\Large\scshape}{\theleftsection \qquad #1}{0em}{~}
\titlespacing{\leftsection}{0pt}{0pt}{0pt}

% Right section
\titleclass{\rightsection}{straight}[\section]
\newcounter{rightsection}
\titleformat{\rightsection}{\Large\scshape}{#1 \qquad \therightsection}{0em}{~}
\titlespacing{\rightsection}{0pt}{0pt}{0pt}


\begin{document}

\leftsection{Left Section}
\lipsum[5]
\newpage

\rightsection{Right Section}
\lipsum[5]

\end{document}

行人尝试创建不同的左、右部分标题

答案1

我假设\leftsection并且\rightsection正在做你想做的事。

% !TeX program = lualatex
\documentclass[english,a4paper,10pt,twocolumn]{article}

\usepackage[english]{babel}
\usepackage[T1]{fontenc} 
\usepackage[explicit]{titlesec}
\usepackage{lipsum}

% Left section
\titleclass{\leftsection}{straight}[\section]
\newcounter{leftsection}
\titleformat{\leftsection}{\Large\scshape}{\theleftsection \qquad #1}{0em}{~}
\titlespacing{\leftsection}{0pt}{0pt}{0pt}

% Right section
\titleclass{\rightsection}{straight}[\section]
\newcounter{rightsection}
\titleformat{\rightsection}{\Large\scshape}{#1 \qquad \therightsection}{0em}{~}
\titlespacing{\rightsection}{0pt}{0pt}{0pt}

\makeatletter
\newcommand{\mysection}[1]% #1 = title
{\par\if@firstcolumn\leftsection{#1}%
 \else\rightsection{#1}%
 \fi}
\makeatother

\begin{document}
\mysection{Left Section}
\lipsum[5]
\newpage

\mysection{Right Section}
\lipsum[5]

\end{document}

答案2

您可以\section直接使用,尽管在极端情况下可能结果是错误的。为了最大程度的安全,可能\needspace应该在 的开头添加一些说明\section

\documentclass[english,a4paper,10pt,twocolumn]{article}

\usepackage[english]{babel}
\usepackage[T1]{fontenc} 
\usepackage{titlesec}
\usepackage{lipsum}

\titleformat{\section}
  {\Large\scshape}
  {}
  {0pt}
  {\formatsection}
\titleformat{name=\section,numberless}
  {\Large\scshape}
  {}
  {0pt}
  {\formatsectionnonumber}

\makeatletter
\newcommand{\formatsection}[1]{%
  \if@firstcolumn
    \thesection\qquad#1\filright
  \else
    \filleft #1\qquad\thesection
  \fi
}
\newcommand{\formatsectionnonumber}[1]{%
  \if@firstcolumn
    #1\filright
  \else
    \filleft #1%
  \fi
}
\makeatother

%\titlespacing{\section}{0pt}{0pt}{0pt}


\begin{document}

\section*{Left Section}

\lipsum[1]

\section{Left Section}
\lipsum[2-4]

\section{Right Section}
\lipsum[5]

\section*{Right Section}

\lipsum[6]

\end{document}

在此处输入图片描述

needspace

\documentclass[english,a4paper,10pt,twocolumn]{article}

\usepackage[english]{babel}
\usepackage[T1]{fontenc} 
\usepackage{titlesec}
\usepackage{etoolbox,needspace}
\usepackage{lipsum}

\titleformat{\section}
  {\Large\scshape}
  {}
  {0pt}
  {\formatsection}
\titleformat{name=\section,numberless}
  {\Large\scshape}
  {}
  {0pt}
  {\formatsectionnonumber}

\preto\section{\needspace{5\baselineskip}}

\makeatletter
\newcommand{\formatsection}[1]{%
  \if@firstcolumn
    \thesection\qquad#1\filright
  \else
    \filleft #1\qquad\thesection
  \fi
}
\newcommand{\formatsectionnonumber}[1]{%
  \if@firstcolumn
    #1\filright
  \else
    \filleft #1%
  \fi
}
\makeatother

%\titlespacing{\section}{0pt}{0pt}{0pt}


\begin{document}

\section*{Left Section}

\lipsum[1]

\section{Left Section}
\lipsum[2-4]

\section{Right Section}
\lipsum[5]

\section*{Right Section}

\lipsum[6]

\end{document}

相关内容