章节标题:带标签和不带标签的彩色盒子

章节标题:带标签和不带标签的彩色盒子

我需要在 LaTex 中重现学术期刊的图形布局。至于章节标题规则,我使用以下代码获得了所需的结果:

\documentclass[10pt]{article}
\usepackage[T1]{fontenc}
\usepackage[utf8]{inputenc}
\usepackage[a4paper, left=35mm,right=15mm,top=15mm,bottom=15mm]{geometry}

\usepackage{palatino,helvet}
\usepackage[dvipsnames]{xcolor}
\usepackage{graphicx}

\newcommand{\Desired}[2]{
    \begin{flushleft}
    \vspace{10mm}
    \fontsize{12}{12}\selectfont\noindent
    \noindent\hspace{-20mm}\raisebox{0.6mm}{\colorbox{BaseColor!70}{%
    \parbox{16mm}{%
    \hfill\vphantom{M}}} }%
    %
    \fontsize{14}{16.8}\selectfont
    \hspace{-21mm}\raisebox{0.5mm}{
    \parbox{18mm}{%
    \hfill\color{white}\vphantom{0}\textbf{\textit{#1\ifx&#1& \else . \fi}}}}
    \hspace{1mm}\textsf{\textbf{#2}}
    \end{flushleft}
    \normalsize\vspace{3mm}
}

\usepackage{lipsum}
\definecolor{BaseColor}{cmyk}{0, 0.71, 0.22, 0.02}

\begin{document}

\Desired{}{Unnumbered Section}
\lipsum[1][1-3]
\Desired{1}{Numbered Section}
\lipsum[2][1-3]

\end{document}

结果:

实际结果

我尝试使用以下命令传输此格式\titlesec\titleformat

\documentclass[10pt]{article}
\usepackage[T1]{fontenc}
\usepackage[utf8]{inputenc}
\usepackage[a4paper, left=35mm,right=15mm,top=15mm,bottom=15mm]{geometry}

\usepackage{palatino,helvet}
\usepackage[dvipsnames]{xcolor}
\usepackage{graphicx}
\usepackage[explicit]{titlesec}

\newcommand\numbered{%
    \hspace{-22.5mm}\raisebox{0.5mm}{
    \parbox{18mm}{%
    \hfill\color{white}\vphantom{0}\textbf{\textit{\thesection.}}}}
}

\newcommand\basebox{%
    \hspace{-21.5mm}\raisebox{0.5mm}{\colorbox{BaseColor!70}{%
    \parbox{18mm}{%
    \hfill\vphantom{0}}} }
}

\titleformat{\section}
    {\vspace{6mm}\fontsize{14}{16.8}\selectfont\bfseries\flushleft\basebox }
    {\numbered}{0em}{\textsf{\textbf{#1}}}
    {}
    {\par\normalsize\vspace{4mm}}

\usepackage{lipsum}
\definecolor{BaseColor}{cmyk}{0, 0.71, 0.22, 0.02}

\begin{document}

\section*{Unnumbered Section}
\lipsum[1][1-3]
\section{Numbered Section}
\lipsum[2][1-3]

\end{document}

但我没有得到预期的结果,最重要的是我得到了错误<format> 参数不能包含水平材质。我不知道在哪里可以添加必要的代码。我也查看了有关重新定义的文档和示例\section,但在我看来,即使在那里,我在格式方面也存在局限性。

答案1

您应该将形状更改为block允许在垂直模式下排版。我简化了代码的一些小部分:

\documentclass[10pt]{article}
\usepackage[a4paper, left=35mm, right=15mm, top=15mm, bottom=15mm]{geometry}

\usepackage{palatino, helvet}
\usepackage[dvipsnames]{xcolor}
\usepackage{graphicx}
\usepackage[explicit]{titlesec}

\newcommand\numbered{%
    \hspace{-20mm}\raisebox{0.5mm}{%
        \parbox{18mm}{%
            \hfill\color{white}\vphantom{0}\textbf{\textit{\thesection.}}%
        }%
    }\hspace{2mm}%
}

\newcommand\basebox{%
    \hspace{-21.5mm}\raisebox{0.5mm}{%
        \colorbox{BaseColor!70}{%
            \parbox{18mm}{%
                \hfill\vphantom{0}%
            }%
        }%
    }
}

\titleformat{\section}[block]
    {\vspace{6mm}\fontsize{14}{16.8}\selectfont\bfseries\basebox}
    {\numbered}{0pt}{\sffamily\bfseries{#1}}
    {}
    {\vspace{4mm}}

\usepackage{lipsum}
\definecolor{BaseColor}{cmyk}{0, 0.71, 0.22, 0.02}

\begin{document}

\section*{Unnumbered Section}
\lipsum[1][1-3]
\section{Numbered Section}
\lipsum[2][1-3]

\end{document}

在此处输入图片描述

答案2

在下面的代码中,我注释掉了\raisebox{0.5mm}深奥的个人品味。

\documentclass[10pt]{article}
\usepackage[T1]{fontenc}
\usepackage[utf8]{inputenc}
\usepackage[a4paper, left=35mm,right=15mm,top=15mm,bottom=15mm]{geometry}

\usepackage{palatino,helvet}
\usepackage[dvipsnames]{xcolor}
\usepackage{graphicx}
\usepackage[explicit]{titlesec}

\newcommand\numbered{%
    \leavevmode % seems titlesec already in horizontal mode anyhow
    \llap{%\raisebox{0.5mm}{%
            \colorbox{BaseColor!70}{\makebox[18mm]{%
                      \hfill\bfseries\itshape
                      \textcolor{white}{\thesection.}}}%
            \hspace{4.5mm}%
           %}% commented-out raisebox
          }%
    \global\let\basebox\empty
}

\newcommand\basebox{%
    \leavevmode\llap{%\raisebox{0.5mm}{%
            \colorbox{BaseColor!70}{\makebox[18mm]{%
                      \hfill\bfseries\itshape
                      \vphantom{0}}}%
            \hspace{4.5mm}%
                     %}% commeneted-out raisebox
     }%
}
\let\Basebox\basebox

\titleformat{\section}
    {\vspace{6mm}\fontsize{14}{16.8}\selectfont}
    {\numbered}
    {0em}
    {\basebox\textsf{\textbf{#1}}\global\let\basebox\Basebox}
    {}
    {\par\normalsize\vspace{4mm}}

\usepackage{lipsum}
\definecolor{BaseColor}{cmyk}{0, 0.71, 0.22, 0.02}

\begin{document}

\section*{Unnumbered Section}
\lipsum[1][1-3]
\section{Numbered Section}
\lipsum[2][1-3]
\section*{Unnumbered Section}
\lipsum[3][1-3]
\section{Numbered Section}
\lipsum[4][1-3]

\end{document}

部分标签

相关内容