如何拉伸行的邊距並防止最後一個元素斷行?

如何拉伸行的邊距並防止最後一個元素斷行?

編輯 1:圖像應該是“三行而不是四行”。

編輯 2:更新了可編譯程式碼。

我是 Latex 新手,我有以下一段模板程式碼,為我的論文提供了標題:


\documentclass[headsepline,footsepline,footinclude=false,oneside,fontsize=11pt,paper=a4,listof=totoc,bibliography=totoc]{scrbook} % one-sided
\usepackage{graphicx}
\usepackage[ngerman,american]{babel}
\newcommand*{\getUniversity}{University of Earth}
\newcommand*{\getFaculty}{Engineering Department}
\newcommand*{\getDegree}{Computer Science}
\newcommand*{\getSchool}{School of Public Science}
\newcommand*{\getTitle}{This is a Long title that I want to stretch the Margin so that instead of 4 lines, it should only be 3 lines, and the logo at the end won't break}
\newcommand*{\getTitleGer}{This is a Long title that I want to stretch the Margin so that instead of 4 lines, it should only be 3 lines, and the logo at the end won't break}
\newcommand*{\getAuthor}{Author}
\newcommand*{\getDoctype}{Thesis}
\newcommand*{\getSupervisor}{Faculty Professor}
\newcommand*{\getAdvisor}{Faculty Advisr}
\newcommand*{\getSubmissionDate}{\today}
\newcommand*{\getSubmissionLocation}{Somewhere In between}

\begin{document}

\begin{titlepage}
  \centering

    \includegraphics[height=20mm]{example-image-a}
  

  \vspace{5mm}
  {\huge\MakeUppercase{School of \getSchool{} --- \getFaculty{}} \par}

  \vspace{5mm}
  {\large\MakeUppercase{\getUniversity{}} \par}

  \vspace{20mm}
  % \vspace{6mm}
  {\Large \getDoctype{} in \getDegree{} \par}

  \vspace{15mm}
  % \vspace{4mm}
  {\huge\bfseries \getTitle{} \par}

  \vspace{5mm}
  {\huge\bfseries \foreignlanguage{ngerman}{\getTitleGer{}} \par}

  \vspace{10mm}
  \begin{tabular}{l l}
    Author:          & \getAuthor{}         \\
    Supervisor:      & \getSupervisor{}     \\
    Advisors:         & \getAdvisor{}        \\
    Submission Date: & \getSubmissionDate{} \\
  \end{tabular}
  
  
    \vfill{}
    \includegraphics[height=15mm]{example-image-b}
  
\end{titlepage}



\end{document}

我有兩個問題:

  1. 有沒有辦法增加 \getTitle{} 部分的行邊距?我想保留字體大小和 vspace{} 規格。隨著標題變長,它往往會很快分成 3-4 行。我想將其拉伸到大約 2-3 行。

  2. 如果標題太長,作者身分部分和最後部分faculty_logo(程式碼片段中的圖像範例 B)將插入下一頁。有沒有辦法始終將這兩個元件固定在頁面底部而不移動到下一頁?

總的來說,我想要 2 個標題,每個標題有 3 行,在程式碼片段中定義相同的文字長度,並且所有內容都應該保留在同一頁上。

答案1

也許一個宏就足夠了,它可以產生\parbox突出到邊緣的 :

\documentclass[headsepline,footsepline,footinclude=false,oneside,fontsize=11pt,paper=a4,listof=totoc,bibliography=totoc]{scrbook} % one-sided

%---------------------------------------------------------
\newsavebox\scratchbox
\newcommand\boxIntoMargins[4]{%
  \savebox\scratchbox{%
     \mbox{%
       \kern-\dimexpr(#1)\relax
       \parbox[b]{\dimexpr((#1)+(#2)+(#3))\relax}{#4}%
     }%
  }%
  \wd\scratchbox=\linewidth
  \usebox\scratchbox
}%
%---------------------------------------------------------


\usepackage{graphicx}
\usepackage[ngerman,american]{babel}
\newcommand*{\getUniversity}{University of Earth}
\newcommand*{\getFaculty}{Engineering Department}
\newcommand*{\getDegree}{Computer Science}
\newcommand*{\getSchool}{School of Public Science}
\newcommand*{\getTitle}{%
  \boxIntoMargins{1.5cm}{\linewidth}{1.5cm}{%
    \centering
    This is a Long title that I want to stretch the Margin so that instead of 4 lines, it should only be 3 lines, and the logo at the end won't break%
  }%
}
\newcommand*{\getTitleGer}{%
  \boxIntoMargins{1.5cm}{\linewidth}{1.5cm}{%
    \centering
    This is a Long title that I want to stretch the Margin so that instead of 4 lines, it should only be 3 lines, and the logo at the end won't break%
  }%
}
\newcommand*{\getAuthor}{Author}
\newcommand*{\getDoctype}{Thesis}
\newcommand*{\getSupervisor}{Faculty Professor}
\newcommand*{\getAdvisor}{Faculty Advisr}
\newcommand*{\getSubmissionDate}{\today}
\newcommand*{\getSubmissionLocation}{Somewhere In between}

\begin{document}

\begin{titlepage}
  \centering

    \includegraphics[height=20mm]{example-image-a}
  

  \vspace{5mm}
  {\huge\MakeUppercase{School of \getSchool{} --- \getFaculty{}} \par}

  \vspace{5mm}
  {\large\MakeUppercase{\getUniversity{}} \par}

  \vspace{20mm}
  % \vspace{6mm}
  {\Large \getDoctype{} in \getDegree{} \par}

  \vspace{15mm}
  % \vspace{4mm}
  {\huge\bfseries \getTitle{} \par}

  \vspace{5mm}
  {\huge\bfseries \foreignlanguage{ngerman}{\getTitleGer{}} \par}

  \vspace{10mm}
  \begin{tabular}{l l}
    Author:          & \getAuthor{}         \\
    Supervisor:      & \getSupervisor{}     \\
    Advisors:         & \getAdvisor{}        \\
    Submission Date: & \getSubmissionDate{} \\
  \end{tabular}
  
  
    \vfill{}
    \includegraphics[height=15mm]{example-image-b}
  
\end{titlepage}

\end{document}

使用 Overleaf 編譯的截圖:

在此輸入影像描述



在補充評論中提出了以下問題:

我忘記再次調整模板的預設值。圖像b的高度應該是20mm,仍然斷到第二頁。有沒有辦法讓它始終保持在一頁上?

該答案最初發布的作者猜測,沒有辦法始終將內容保留在單一頁面上,因為在單一頁面上容納太多文字的情況總是可以透過添加更多文字來滿足。 ;-) 他認為嘗試自動化調整以增加將內容放在單一頁面上的機會可能不值得。

您可以將標題頁的內容放入單獨的內容中,其中內容可能超出紙張的尺寸,並結合使用Graphicxs 包中的\newsavebox命令和縮小框以適合紙張的命令。\scalebox\usebox

或者將標題頁放在一個單獨的 .tex 文件中,從中創建一個單獨的 .pdf 文件,然後將其包含到您的主文檔中,透過\includegraphics縮小比例以適應主文檔紙張的大小可以做完了。在單獨的 .tex 文件中,有一個裝有標題頁的盒子,該標題頁超出文檔紙張的尺寸,在調整\(pdf)pagewidth和後將被運出\(pdf)pageheight
以下可能是如何以這種方式處理問題的概述:

\documentclass{article}
\newbox\BoxWithTitlepage
\newcommand\ShipoutAsPage[1]{%
  \begingroup
  \setbox\BoxWithTitlepage=\vbox{%
    \hrule height 0pt
    #1\ifvmode\else\expandafter\endgraf\fi
    \hrule height 0pt
  }%
  \csname @ifundefined\endcsname{pagewidth}{}{\pagewidth=\wd\BoxWithTitlepage}%
  \csname @ifundefined\endcsname{pdfpagewidth}{}{\pdfpagewidth=\wd\BoxWithTitlepage}%
  \csname @ifundefined\endcsname{pageheight}{}{\pageheight=\ht\BoxWithTitlepage
                                               \advance\pageheight\dp\BoxWithTitlepage}%
  \csname @ifundefined\endcsname{pdfpageheight}{}{\pdfpageheight=\ht\BoxWithTitlepage
                                                  \advance\pdfpageheight\dp\BoxWithTitlepage}%
  \shipout\vbox{\kern-1truein\hbox{\kern-1truein\box\BoxWithTitlepage}}%
  \endgroup  
}%
\begin{document}
\ShipoutAsPage{%
  % Content of your titlepage
  % Content of your titlepage
  % Content of your titlepage
  % Content of your titlepage
}%
\end{document}

但有充分的理由說明為什麼不能真正推薦這樣做:

  • 這樣,您就無法輕鬆地從標題頁獲得到文件其他位置的超連結。
  • 縮放看起來不太好,因為字體不再是其自然大小。
  • 當將事物縮放到較小尺寸時,例如用環境建立的表格或圖像等的規則/線tikzpicture可能會變得太細,因此可能無法列印。

因此,您可能最好「手動」調整一些內容,例如,將標題頁的字體區域向紙張底部放大一點,並將字距向上一點。以下是一個“快速但骯髒”的解決方案,但可能會達到目的:

\documentclass[headsepline,footsepline,footinclude=false,oneside,fontsize=11pt,paper=a4,listof=totoc,bibliography=totoc]{scrbook} % one-sided

%---------------------------------------------------------
\newsavebox\scratchbox
\newcommand\boxIntoMargins[4]{%
  \savebox\scratchbox{%
     \mbox{%
       \kern-\dimexpr(#1)\relax
       \parbox[b]{\dimexpr((#1)+(#2)+(#3))\relax}{#4}%
     }%
  }%
  \wd\scratchbox=\linewidth
  \usebox\scratchbox
}%
%---------------------------------------------------------


\usepackage{graphicx}
\usepackage[ngerman,american]{babel}
\newcommand*{\getUniversity}{University of Earth}
\newcommand*{\getFaculty}{Engineering Department}
\newcommand*{\getDegree}{Computer Science}
\newcommand*{\getSchool}{School of Public Science}
\newcommand*{\getTitle}{%
  \boxIntoMargins{1.5cm}{\linewidth}{1.5cm}{%
    \centering
    This is a Long title that I want to stretch the Margin so that instead of 4 lines, it should only be 3 lines, and the logo at the end won't break%
  }%
}
\newcommand*{\getTitleGer}{%
  \boxIntoMargins{1.5cm}{\linewidth}{1.5cm}{%
    \centering
    This is a Long title that I want to stretch the Margin so that instead of 4 lines, it should only be 3 lines, and the logo at the end won't break%
  }%
}
\newcommand*{\getAuthor}{Author}
\newcommand*{\getDoctype}{Thesis}
\newcommand*{\getSupervisor}{Faculty Professor}
\newcommand*{\getAdvisor}{Faculty Advisr}
\newcommand*{\getSubmissionDate}{\today}
\newcommand*{\getSubmissionLocation}{Somewhere In between}

\begin{document}

\begin{titlepage}
  \enlargethispage{1cm}%
  \hrule height 0mm
  \kern-.5cm

  \centering

    \includegraphics[height=20mm]{example-image-a}
  

  \vspace{5mm}
  {\huge\MakeUppercase{School of \getSchool{} --- \getFaculty{}} \par}

  \vspace{5mm}
  {\large\MakeUppercase{\getUniversity{}} \par}

  \vspace{20mm}
  % \vspace{6mm}
  {\Large \getDoctype{} in \getDegree{} \par}

  \vspace{15mm}
  % \vspace{4mm}
  {\huge\bfseries \getTitle{} \par}

  \vspace{5mm}
  {\huge\bfseries \foreignlanguage{ngerman}{\getTitleGer{}} \par}

  \vspace{10mm}
  \begin{tabular}{l l}
    Author:          & \getAuthor{}         \\
    Supervisor:      & \getSupervisor{}     \\
    Advisors:         & \getAdvisor{}        \\
    Submission Date: & \getSubmissionDate{} \\
  \end{tabular}
  
  
    \vfill{}
    \includegraphics[height=20mm]{example-image-b}
  
\end{titlepage}

\end{document}

在此輸入影像描述

如果⟨height-of-top-margin⟩:⟨height-of-bottom-margin⟩ 的比率不是1:1,您可能需要進行更多計算,以確保在垂直向上字距調整的同時向下擴大文字區域的比率至今仍被保留。

如果比率 ⟨width-of-margin-at-the-left⟩:⟨width-of-margin-at-the-right⟩ 不是 1:1,您可能需要進行更多計算以確保

\boxIntoMargins{<amount of horizontal space that sticks into left margin>}%
               {\linewidth}%
               {<amount of horizontal space that sticks into right margin>}%
               {...}%

該比例被保留。


也許對於該行來說,
{\huge\MakeUppercase{School of \getSchool{} --- \getFaculty{}} \par}
當事情安排好時,它可能看起來不錯,這樣萬一在第一個破折號之前發生換行,並且表示教師的整個字符串本身就適合在一行上。後面附加一個破折號\getFaculty{},以便教師字串嵌套在兩個破折號之間。 ;-)


順便說一句:你的例子的程式碼讓我想起封面頁和標題頁附有剪貼簿,標題頁後面有不需要的空白頁。也許您會在那裡找到一些有用的見解。

相關內容