![根据左列或右列的位置定义自定义部分命令](https://linux22.com/image/350409/%E6%A0%B9%E6%8D%AE%E5%B7%A6%E5%88%97%E6%88%96%E5%8F%B3%E5%88%97%E7%9A%84%E4%BD%8D%E7%BD%AE%E5%AE%9A%E4%B9%89%E8%87%AA%E5%AE%9A%E4%B9%89%E9%83%A8%E5%88%86%E5%91%BD%E4%BB%A4.png)
我正在尝试使用\section
该titlesec
包定制命令,以便结果会根据节标题位于左列还是右列而有所不同。
理想情况下,我想要一些类似于下面的 MWE 的东西,但是其中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}
\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}