2

Is there a way to know the width used by a paragraph ? In the following code, I would like to adjust the line to be a little bigger than the text "Location: Somewhere over the rainbow".

\documentclass[12pt]{article}
\usepackage[showframe]{geometry}


\begin{document}

\setlength{\parindent}{0pt}

\begin{center}
\rule[0.4ex]{\linewidth}{0.2ex}

DS: Probability \& Geometry

Location: Somewhere over the rainbow

\rule[0.4ex]{\linewidth}{0.2ex}
\end{center}

Bla, bla, bla, bla, bla, bla, ...

\end{document}
projetmbc
  • 13,315
  • 1
    You can measure the width using something like: \setbox0\hbox{Location: Somewhere over the rainbow} the width is then \wd0. Note that this is not expandable and you should evaluate \wd0 close to the \setbox because it will be invalid if something else uses that box. – Skillmon Nov 21 '17 at 19:19
  • Let's see my comment to the answer of Latain. – projetmbc Nov 21 '17 at 21:47

2 Answers2

4

like this?

enter image description here

\documentclass[12pt]{article}
\usepackage[showframe]{geometry}
\usepackage{booktabs}

\begin{document}
    \begin{center}
\begin{tabular}{c}
    \midrule
DS: Probability \& Geometry             \\
Location: Somewhere over the rainbow    \\
    \midrule
\end{tabular}
    \end{center}

Bla, bla, bla, bla, bla, bla, ...

\end{document}

edit: if you like to change midrule thickness, you can add option with thickness as for example \midrule[3pt]. also for top rule you can use \toprule or \toprule[2pt]. for more details see documentation of package booktabs.

rules are longer than text for \tabcolsep on each side of table cell. its default size is 6pt, but you can change size with \setlength\tabcolsep{<desired length>}. An example considering this is:

\documentclass[12pt]{article}
\usepackage[showframe]{geometry}
\usepackage{booktabs}

\begin{document}
    \begin{center}
    \setlength\tabcolsep{12pt}
\begin{tabular}{c}
    \toprule[2pt]
DS: Probability \& Geometry             \\
Location: Somewhere over the rainbow    \\
    \midrule[3pt]
\end{tabular}
    \end{center}

Bla, bla, bla, bla, bla, bla, ...

\end{document}

enter image description here

If you want to set a factor, say 20%, of overshoot,

\documentclass[12pt]{article}
\usepackage[showframe]{geometry}
\usepackage{booktabs}

\begin{document}

\begin{center}
\begin{tabular}{c}
\toprule[2pt]
\makebox[1.20\width]{% 20% longer rules
  \begin{tabular}{@{}c@{}}
    DS: Probability \& Geometry \\
    Location: Somewhere over the rainbow
  \end{tabular}%
}\\
\midrule[3pt]
\end{tabular}
\end{center}

Bla, bla, bla, bla, bla, bla, ...

\end{document}

enter image description here

egreg
  • 1,121,712
Zarko
  • 296,517
  • Thanks but is there a way to set the width using a factor, and thickness using absolute dimension ? – projetmbc Nov 21 '17 at 20:55
  • width of midrule? for example: \midrule[1pt], length ofmidrule? it is longer than text for\tabcolsepon each side. you can change it by for example\setlength\tabcolsep{12pt} (default value of\tabcolsep` is 6pt). – Zarko Nov 21 '17 at 21:49
  • I took the liberty of adding a method for stating the amount of overshoot as a factor of the width of the text. – egreg Nov 21 '17 at 22:14
  • @eqreg, thank you. i should confess, that i didn't understood, what op means with " set the width using a factor" ... now have sense even for me :-) – Zarko Nov 21 '17 at 22:21
  • Sorry for my english. I'm a french frog. ;-) Thanks a lot. This seems to be the simpler way to achieve what I need. – projetmbc Nov 21 '17 at 23:00
1

You can also use \widthof{} from the usepackage calc like: \rule[0.4ex]{\widthof{Location: Somewhere over the rainbow}}{0.2ex}

Latain
  • 51
  • This can donne the job but I will need then to calculate a maximum regarding each line of my text. Is there an easy way to do that ? – projetmbc Nov 21 '17 at 20:56