0

I'm writing a thesis template, I want to underline and centering the title. One line has a length of 5cm. When the title is too long to type in one single line, I have to put it in two, three even more lines, like this

enter image description here

I want to breaklines automatically, is there any good ideas? I simplify my demand (maybe not) and I put my ideas below, but I met many problems.

I have a tring a, which is somewordssomewords.

\newcommand{\a}{somewordssomewords}

I want do cut it into some pieces, which length is 5, and underline them seperately, like

enter image description here

I want to do this as follow:

  1. use \StrLen{\a} from xstring package to get \a's length, which is 18
  2. use \fpeval{\StrLen{\abc}/5} from xfp package to get how many pieces I should cut, in this example, we denote it as 3.6
  3. and I want to use \foreach from pgffor package and \underline{\StrMid{\a}{5*(\x-1)+1}{5*\x}} to get underlined "somew", "ordss", "omewo", and "rds"

My problem are,

  1. I can't use \fpeval{\alen/5}
\documentclass{article}
\usepackage{xfp, xstring}
\newcommand{\abc}{somewordssomewords}
\begin{document}
    \fpeval{\StrLen{\abc}/5}
\end{document}

error is

! You can't use `\let' after \the.
<argument> \let

l.5     \fpeval{\StrLen{\abc}/5}

?

How can I get how many pieces I shout cut?

  1. How to use \x in \foreach to calculate. For example, use something like this
\foreach \x in {1, 2, 3}{2*\x}

to get 246.

  1. Is there some loop sentence like
while \StrLen{\a} > 5
  cut left 5 words and underline
  \a = string cutted the left 5 words
end

If there is, my work will be easy

1
  • 1
    \StrLen isn't expandable so you can't use it inside \fpeval.
    – Skillmon
    Commented Aug 3, 2021 at 8:41

3 Answers 3

2

As long as no changes of \baselineskip and no vertical gaps occur within your text you probably can use adjustbox for measuring the height of your box with the centered text and placing another box of same measurements, containing vertical leaders made up of horizontal leaders for creating the lines.

This way lines are drawn over the text. Therefore I cannot recommend using this if color of text and color of lines differ.

\documentclass[12pt]{article}
\usepackage{adjustbox}
\newlength\myheight
\newcommand\Mysavedprevdepth{}%
\newcommand\UnderlineCentered[3]{%
  \begin{adjustbox}{minipage=[t]{\dimexpr#1\relax},gstore totalheight=\myheight,margin=0pt}%
  \centering\leavevmode#3\par\xdef\Mysavedprevdepth{\the\prevdepth}%
  \end{adjustbox}%
  \hspace*{-\dimexpr#1\relax}%
  \begin{adjustbox}{minipage=[t][\myheight]{\dimexpr#1\relax},margin=0pt}%
  \vphantom{Eg}\lower\dimexpr#2\relax\hbox to\hsize{\leaders\hrule\hfill\kern0pt}\par
  \kern-\dimexpr#2\relax
  \xleaders\vbox to\baselineskip {\vfill\hbox{\lower\dimexpr#2\relax\hbox to\hsize{\leaders\hrule\hfill\kern0pt}}\kern-\dimexpr#2\relax}\vfill
  \kern\Mysavedprevdepth
  \end{adjustbox}%
}%

\usepackage{lipsum}
\begin{document}

\noindent\xleaders\hbox{X}\hfill\null

\bigskip

\begingroup\centering\UnderlineCentered{5cm}{1mm}{\lipsum[12]}\par\endgroup

\bigskip

\noindent\xleaders\hbox{X}\hfill\null

\bigskip

\UnderlineCentered{5cm}{1mm}{AAA}

\bigskip

\UnderlineCentered{5cm}{1mm}{}

\end{document}

enter image description here

3

This puts the formatted title into a savebox, uses \vsplit in a loop to break it into lines and \hrule to underline them.

\documentclass{article}

\newcommand{\foo}[1]% #1 = title
{\par\noindent\hfil\parbox{5cm}{%
  \setbox0=\vbox{\centering #1}%
  \loop\ifvoid0\else
    \setbox1=\vsplit0 to \ht\strutbox
    \unvbox1\hrule
  \repeat}\par}

\begin{document}
\foo{This is a long sentence to complete}
\end{document}

demo


This version uses varwidth to reduce the width when possible.

\documentclass{article}
\usepackage{varwidth}

\newcommand{\foo}[1]% #1 = title
{\bgroup% use local registers
 \sbox0{\begin{varwidth}{5cm}
    \centering #1
   \end{varwidth}}
  \par\noindent\hfil\parbox{\wd0}{%
  \setbox0=\vbox{\centering #1}%
  \loop\ifvoid0\else
    \setbox1=\vsplit0 to \ht\strutbox
    \unvbox1\hrule
  \repeat}\par
\egroup}

\begin{document}
\foo{This is a long sentence to complete}
\end{document}

demo 2

2

Instead of programmatically breaking the lines with some TeX code that uses \underline directly you might want to consider using one of the following packages allowing line breakable underlining

  • ulem (no automatic hyphenation)
  • soul (automatic hyphenation but fragile for some input)
  • lua-ul (only available in LuaLaTeX, but best of these by far)

None of those would put a line from either end of the text block to the other, instead they would only underline your words.

\documentclass[]{article}

\newcommand\showulresult[2]
  {%
    \par
    Using \textsf{#1}:\par
    \fbox{\begin{minipage}{5cm}
      \centering\huge\bfseries
      #2%
        {%
          This is a long sentence to complete with
          Supercalifragilisticexpialidocious%
        }
    \end{minipage}}%
    \bigskip
  }

\usepackage{soul}
\usepackage[normalem]{ulem}
\usepackage{lua-ul}

\begin{document}
\showulresult{soul}{\ul}
\showulresult{ulem}{\uline}
\showulresult{lua-ul}{\underLine}

Using \textsf{ulem} with manual hyphenation:\par\nobreak
\fbox{\begin{minipage}{5cm}
  \centering\huge\bfseries
  \uline
    {%
      This is a long sentence to complete with
      Super\-cali\-fragi\-listic\-expi\-ali\-docious%
    }
\end{minipage}}%
\bigskip
\end{document}

enter image description here

enter image description here

1
  • Well, I didn't descirbe my demand clearly, I mainly want to the all underlines share the longest length, like the fist figure in my question.
    – Syvshc
    Commented Aug 3, 2021 at 9:33

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .