我定义了一个新命令来为新闻稿制作一些标题。我正在使用 article 类作为文档。该命令使用以下代码定义:
\definecolor{myblue}{rgb}{0.16, 0.32, 0.75}
\newcommand{\articletitle}[1]
{
\stepcounter{art}
\setcounter{section}{0}
\vspace{0.15cm}
{\raggedright\fcolorbox{myblue}{myblue}{{\huge \textcolor{white}{\arabic{art}}}}\hspace{1em}\textcolor{myblue}{\huge{#1}}}\\
\rule{\textwidth}{1pt}
\vspace{-0.25cm}
}
这给出了预期的结果:
问题在于标题大于一行时,会出现以下情况:
我希望标题的第二行与第一个字母对齐,并且与第一个字母和之间的间距相同\fcolorbox
。
结果应如下所示:
我搜索了如何做到这一点,但没有成功。当然,我相信用 LateX 做这件事相当容易!所以如果有人能帮我...
谢谢
答案1
以下是使用 的解决方案tabularx
。我认为对于悬挂标题,最好让它们右侧不整齐:
\documentclass[a4paper,twoside,11pt]{article}
\usepackage[utf8]{inputenc}
\usepackage[T1]{fontenc} %
\usepackage{helvet}
\renewcommand\thepage{{\sffamily\arabic{page}}}
\usepackage{tabularx}
\usepackage{xcolor}
\newcounter{art}[section]
\setcounter{art}{0}
\definecolor{myblue}{rgb}{0.16, 0.32, 0.75}
\newcommand\labelart{\raisebox{-\height}{\fcolorbox{myblue}{myblue}{\centering\textcolor{white}{\,\arabic{art}\,}}}}
\newcommand{\articletitle}[1]
{%
\stepcounter{art}
\setcounter{section}{0}
\vspace{0.15cm}
{\LARGE\noindent\begin{tabularx}{\linewidth}{@{}l @{\enspace}>{\raggedright\color{myblue}}X@{}}
\labelart & #1
\end{tabularx}}\\
\textcolor{myblue}{\rule{\linewidth}{1pt}}
\vspace{-0.25cm}
}
\usepackage{lipsum}
\begin{document}
\sffamily
\let\defaultfamily\sffamily
\articletitle{This is a very very long title which is properly aligned}
\lipsum[1-2]
\articletitle{This is a shorter title}
\lipsum[3-4]
\end{document}
答案2
这是一个有一些缺点的解决方案,但不清楚 OP 是否可以接受。缺点如下:
1) 多行文章标题右侧参差不齐。
2)必须手动指定换行符
3)由于(2),在将参数添加到目录之前需要特殊规定来删除换行符。
该方法将文章标题置于左对齐的 中\Longunderstack
,其中可能包含手动\\
换行符。标题的行间距当前设置为0.8\baselineskip
,但可以在序言中修改。
\documentclass{article}
\usepackage{xcolor, lipsum}
\usepackage[usestackEOL]{stackengine}
\newcounter{art}
\definecolor{myblue}{rgb}{0.16, 0.32, 0.75}
\newcommand\articletitle[1]
{
\stepcounter{art}
\setcounter{section}{0}
\vspace{0.15cm}
{\raggedright\fcolorbox{myblue}{myblue}{{\huge \textcolor{white}%
{\arabic{art}}}}\hspace{1em}\textcolor{myblue}{\huge{%
\Longunderstack[l]{#1}}}}\\
\rule{\textwidth}{1pt}
\vspace{-0.25cm}
}
\setstackgap{L}{.8\baselineskip}
\begin{document}
\sffamily
\articletitle{This is a test title}
\lipsum[1]
\articletitle{
This is a very long title for the line \\
length which is not properly aligned}
\lipsum[1]
\end{document}
答案3
和titlesec
:
\documentclass[a4paper,twoside,11pt]{article}
\usepackage[utf8]{inputenc}
\usepackage[T1]{fontenc} %
\usepackage{helvet}
\renewcommand\thepage{{\sffamily\arabic{page}}}
\usepackage{titlesec}
\usepackage{xcolor}
\definecolor{myblue}{rgb}{0.16, 0.32, 0.75}
\titleformat{\section}
{\raggedright\LARGE% % <-- add \raggedright
\normalfont\sffamily}
{\fcolorbox{myblue}{myblue}{\textcolor{white}{\thesection}}}
{1em}
{}
%[\vspace{1ex}\titlerule]
[\vspace{-1ex}\rule{\linewidth}{1pt}]
\titlespacing{\section}
{0pc}{*2}{*1}[0pc]
\usepackage{lipsum}
\begin{document}
\sffamily
\let\defaultfamily\sffamily
\section{This is a very very long title which is properly aligned}
\lipsum[1-2]
\section{This is a shorter title}
\lipsum[3-4]
\end{document}