如何将字幕扩展(或调整)至文本宽度?

如何将字幕扩展(或调整)至文本宽度?

我有一个字幕,它几乎和文本宽度一样大。出于美观目的,我想将它拉伸一点,以便覆盖整个文本宽度,并与字幕下方的图片更加协调。我该如何实现这个技巧?

这是显示字幕的代码:

\documentclass[letterpaper]{book}
\usepackage[T1]{fontenc}
\usepackage{microtype}
\usepackage{lmodern}
\usepackage[utf8]{inputenc}
\usepackage[french]{babel}
\usepackage{anysize}
\marginsize{1.5in}{1in}{0.5in}{0.5in}
\usepackage[x11names]{xcolor}
\definecolor{backgroundcolor}{RGB}{180, 180, 160}
\definecolor{subtitle}{RGB}{240, 240, 200}
\usepackage{graphicx}
\usepackage{here}
\usepackage{afterpage}

\usepackage{librebaskerville}
\usepackage{tikz}
\usetikzlibrary{calc}

\newcommand*{\maintitle}{\begingroup

    \pagecolor{backgroundcolor}

    \centering

    \flushleft{
        \textcolor{subtitle}{\Huge\textsf{La théorie classique des champs relativistes}}
    }

    \begin{figure}[H]
        \center
        \includegraphics[width=1\textwidth]{example-image}
    \end{figure}

\endgroup}

\begin{document}

\begin{titlepage}

% FRAME AROUND PAGE :
    \begin{tikzpicture}[remember picture, overlay]
        \draw[line width = 1in,color = white] ($(current page.north west) + (0.25in,0.25in)$) rectangle ($(current page.south east) + (0.25in,-0.25in)$);
        \draw[line width = 0.25pt,color = black] ($(current page.north west) + (0.75in,-0.25in)$) rectangle ($(current page.south east) + (-0.25in,0.25in)$);
        \draw[line width = 1pt,color = white] ($(current page.north west) + (1in,-0.5in)$) rectangle ($(current page.south east) + (-0.5in,0.5in)$);
    \end{tikzpicture}

    \maintitle
\end{titlepage}

\end{document}

预览此代码的作用:

在此处输入图片描述

这里我需要做的就是延长或拉伸字幕,使其长度等于文本宽度。右侧的空间应该与左侧的空间相同(应该保持不变)。


编辑: 我已经更新了上面的代码。也许我不清楚我想要拉伸多少。我当然不希望字幕被拉伸到两边的垂直线。字幕向左对齐,左行和字幕开头之间有一个很好的空间。这应该保持不变。但是,我需要稍微拉伸右侧部分,以获得与下方图片大小相同的字幕。

答案1

我会避免这种情况。

无论如何,您可以这样做。请注意,重音字符和可能的连字符必须用括号括起来。

我做了一些调整:

  1. anysize威力远小于geometry且26年来未曾维持;
  2. here包已过时;float请使用绝不使用[H];
  3. 似乎minipage没有必要,特别是宽度2\textwidth
  4. 要设置大于文本宽度的内容,请使用\makebox
  5. 我在任一尺寸上都添加了一些空间,以便文本不会触及框线。
\documentclass[letterpaper]{book}
\usepackage[inner=1.5in,outer=1in,top=0.5in,bottom=0.5in]{geometry}
%\usepackage[utf8]{inputenc} % not needed with recent versions of LaTeX
\usepackage[T1]{fontenc}
\usepackage[french]{babel}
\usepackage{microtype}
\usepackage{graphicx}
\usepackage[x11names]{xcolor}
\definecolor{backgroundcolor}{RGB}{180, 180, 160}
\definecolor{subtitle}{RGB}{240, 240, 200}

\usepackage{lmodern} % for sans serif
\usepackage{librebaskerville}
\usepackage{tikz}
\usetikzlibrary{calc}

\usepackage{xparse}

\ExplSyntaxOn
\NewDocumentCommand{\expandto}{mm}
 {
  \seq_set_split:Nnn \l_tmpa_seq { ~ } { #2 }
  \seq_set_map:NNn \l_tmpb_seq \l_tmpa_seq { \cham_expand_word:n { ##1 } }
  \leavevmode
  \hbox_to_wd:nn { #1 }
   {
    \hspace{3pt}
    \seq_use:Nn \l_tmpb_seq { \hspace{0.33333em plus 1fil} }
    \hspace{3pt}
   }
 }
\cs_new_protected:Nn \cham_expand_word:n
 {
  \seq_set_split:Nnn \l_tmpa_seq { } { #1 }
  \seq_use:Nn \l_tmpa_seq { \hspace{0pt plus 0.6fil} }
 }
\ExplSyntaxOff

\newcommand*{\maintitle}{%
  \begingroup
  \pagecolor{backgroundcolor}
  \centering
  \makebox[\textwidth]{%
    \color{subtitle}\Huge\sffamily
    \expandto{\textwidth+1in}{La th{é}orie classique des champs relativistes}%
  }%
  \endgroup
}

\begin{document}

\begin{titlepage}

% FRAME AROUND PAGE :
\begin{tikzpicture}[remember picture, overlay]
  \draw[line width = 1in,color = white] ($(current page.north west) + (0.25in,0.25in)$) rectangle ($(current page.south east) + (0.25in,-0.25in)$);
  \draw[line width = 0.25pt,color = black] ($(current page.north west) + (0.75in,-0.25in)$) rectangle ($(current page.south east) + (-0.25in,0.25in)$);
  \draw[line width = 1pt,color = white] ($(current page.north west) + (1in,-0.5in)$) rectangle ($(current page.south east) + (-0.5in,0.5in)$);
\end{tikzpicture}

    \maintitle

\end{titlepage}

\end{document}

在此处输入图片描述

看看这张照片,你就会明白为什么我会以‘我会避免这种情况’开头。;-)

如果标题应该使用文本宽度:

\documentclass[letterpaper]{book}
\usepackage[inner=1.5in,outer=1in,top=0.5in,bottom=0.5in]{geometry}
%\usepackage[utf8]{inputenc} % not needed with recent versions of LaTeX
\usepackage[T1]{fontenc}
\usepackage[french]{babel}
\usepackage{microtype}
\usepackage{graphicx}
\usepackage[x11names]{xcolor}
\definecolor{backgroundcolor}{RGB}{180, 180, 160}
\definecolor{subtitle}{RGB}{240, 240, 200}

\usepackage{lmodern} % for sans serif
\usepackage{librebaskerville}
\usepackage{tikz}
\usetikzlibrary{calc}

\usepackage{xparse}

\ExplSyntaxOn
\NewDocumentCommand{\expandto}{O{0pt}mm}
 {
  \seq_set_split:Nnn \l_tmpa_seq { ~ } { #3 }
  \seq_set_map:NNn \l_tmpb_seq \l_tmpa_seq { \cham_expand_word:n { ##1 } }
  \leavevmode
  \hbox_to_wd:nn { #2 }
   {
    \hspace{#1}
    \seq_use:Nn \l_tmpb_seq { \hspace{0.33333em plus 1fil} }
    \hspace{#1}
   }
 }
\cs_new_protected:Nn \cham_expand_word:n
 {
  \seq_set_split:Nnn \l_tmpa_seq { } { #1 }
  \seq_use:Nn \l_tmpa_seq { \hspace{0pt plus 0.6fil} }
 }
\ExplSyntaxOff

\newcommand*{\maintitle}{%
  \begingroup
  \pagecolor{backgroundcolor}
  \centering
  \makebox[\textwidth]{%
    \color{subtitle}\Huge\sffamily
    \expandto{\textwidth}{La th{é}orie classique des champs relativistes}%
  }\\
  \includegraphics[width=\textwidth]{example-image}%
  \endgroup
}

\begin{document}

\begin{titlepage}

% FRAME AROUND PAGE :
\begin{tikzpicture}[remember picture, overlay]
  \draw[line width = 1in,color = white] ($(current page.north west) + (0.25in,0.25in)$) rectangle ($(current page.south east) + (0.25in,-0.25in)$);
  \draw[line width = 0.25pt,color = black] ($(current page.north west) + (0.75in,-0.25in)$) rectangle ($(current page.south east) + (-0.25in,0.25in)$);
  \draw[line width = 1pt,color = white] ($(current page.north west) + (1in,-0.5in)$) rectangle ($(current page.south east) + (-0.5in,0.5in)$);
\end{tikzpicture}

    \maintitle

\end{titlepage}

\end{document}

在此处输入图片描述

关于您的编辑:

  • \flushleft不是用户级命令,它需要争论;

  • 无需figure在图像周围使用。

我将两侧的填充转换为可选参数,以防\expandto您在其他地方需要它。

相关内容