# Make a variable length horizontal rule depending on the text width of the title

I have the following custom environment for examples:

\documentclass{article}

\usepackage{lipsum}
\usepackage{amsmath, mathtools, amsthm}
\usepackage{graphicx, xcolor}
\usepackage[top=1in, bottom=1in, left=1in, right=1in]{geometry}
\usepackage{changepage} %Indent sections, use of adjustwidth --> http://ctan.org/pkg/changepage

%Environment_____: Example
\newtheorem{example}{Example}[section]
\newcommand{\ex}[3]{
\vbox{\vspace{.2cm}
\noindent{\color{black}\rule{\textwidth}{1pt}}
\vspace{-.8cm}
\begin{example}
\textit{#1.}\\
\rule{.5\textwidth}{0.5pt}\\
#2
\label{#3}
\end{example}
{\color{black}\rule{\textwidth}{1pt}}\vspace{.2cm}
}
}%

\begin{document}
\lipsum[4]

\ex{This is my example}{\lipsum[5]}{ex:MYEX}
\ex{With a longer title, the thin rule does not stand under the whole text}{\lipsum[7]}{ex:MYEX2}
\ex{In the case I put a long long title, I would like to have the thin rule only on the \underline{piece of title closer to her, in this case only under the underlined text}}{\lipsum[3]}{ex:MYEX2}

\end{document}


gives the following output:

Now what I want is the thinnest rule to extend the same width depending on the italic title of the example. In this case, as you can see, the rule is too short to contain the whole title. Is there an automatic way to do it?

Also, I don't want that, in the case I extend the title to more than one line, the rule to double itself by going on another line. Thanks for help.

• Welcome to TeX.SX! Can you please expand the code snippets that you have posted to a full minimal working example. It is much easier to help you you if we can start with some compilable code that illustrates your problem. A MWE should start with a \documentclass command, include any necessary packages and be as small as possible to demonstrate your problem. – Andrew Jul 17 '17 at 22:59
• I read your page for the minimal working example, I'll fix my question to fit the rules! Sorry. – GiuTeX Jul 17 '17 at 23:35
• @Andrew, I made the changes, hope is't more clear... – GiuTeX Jul 17 '17 at 23:53
• At first I thought it could be done using \vsplit to isolate the last line, but instead it includes the blank space to the right. BTW, you might have better luck with \hrule, which has a totally different syntax from \rule but does not act like a line of text. – John Kormylo Jul 18 '17 at 3:35
• Are you willing to compile with LuaTeX? (Using lualatex instead of pdflatex that you're probably using.) Some things are easier in LuaTeX and this looks like it may be one such thing, though I haven't actually tried it… – ShreevatsaR Aug 12 '17 at 23:00

The usual way to measure the last line of a paragraph is to use a hidden display math and inspect \predisplaysize

\documentclass{article}

\usepackage{lipsum}
\usepackage{amsmath, mathtools, amsthm}
\usepackage{graphicx, xcolor}
\usepackage[top=1in, bottom=1in, left=1in, right=1in]{geometry}
\usepackage{changepage} %Indent sections, use of adjustwidth --> http://ctan.org/pkg/changepage

%Environment_____: Example
\newtheorem{example}{Example}[section]
\newcommand{\ex}[3]{
\vbox{\vspace{.2cm}
\noindent{\color{black}\rule{\textwidth}{1pt}}
\vspace{-.8cm}
\begin{example}
\textit{#1.}%
{%
\abovedisplayskip0pt
\abovedisplayshortskip0pt
\belowdisplayskip0pt
\belowdisplayshortskip0pt
$$\xdef\tmp{\the\predisplaysize}$$%
\ifdim\tmp=-\maxdimen
\let\tmp\linewidth
\else
\edef\tmp{\the\dimexpr\tmp-2em+\linewidth-\textwidth
+25pt % from somewhere:-)
\relax}%
\fi
\par
\vskip-\baselineskip
\noindent\rule{\tmp}{0.5pt}\par
}%
\noindent#2
\label{#3}
\end{example}
{\color{black}\rule{\textwidth}{1pt}}\vspace{.2cm}
}
}%

\begin{document}
\lipsum[4]

\ex{This is my example}{\lipsum[5]}{ex:MYEX}
\ex{With a longer title, the thin rule does not stand under the whole text}{\lipsum[7]}{ex:MYEX2}
\ex{In the case I put a long long title, I would like to have the thin rule only on the \underline{piece of title closer to her, in this case only under the underlined text}}{\lipsum[3]}{ex:MYEX2}

\end{document}

• Brilliant solution, just what I was looking for! – GiuTeX Aug 17 '17 at 16:03

This is close to what you want, but not what you have asked for in the case where the title is longer than one line. When the title is longer than one line then I think that you want only the last line to be underlined, but I don't see an easy way to do this. Instead, I have used the \uline command from the ulem package to underline the entire title.

For your MWE, my code produces:

The key points in the code are:

• I have used \newtheoremstyle to define a new theorem style that underlines the theorem name, number and note
• I have used \uline to underline text. From the point of view of this question, the advantage of \uline over \underline is that when given text the runs over multiple lines all of the lines will be underlined. In contrast, \underline{...} forces the text onto a single line that extends into the righthand margin
• To underline the space between the theorem number and the theorem title I have fudged a little by adding a negative \hspace before the title and then adding this \hspace into the underlined title. Btw, I played with making the title equal to the theorem note, but this did not work well with multi-lined titles.

Here is the full code:

\documentclass{article}

\usepackage{lipsum}
\usepackage{amsmath, mathtools, amsthm}
\usepackage{graphicx, xcolor}
\usepackage[top=1in, bottom=1in, left=1in, right=1in]{geometry}
\usepackage{changepage} %Indent sections, use of adjustwidth --> http://ctan.org/pkg/changepage
\usepackage[normalem]{ulem}
% theorem style with theorem-name underlined

%Environment_____: Example
\newtheoremstyle{underlined}%
{}{}%
{\itshape}{}%
{\bfseries}{.}%
{ }%
{\uline{\thmname{#1}\thmnumber{ #2}\thmnote{ (#3)}}}
\theoremstyle{underlined}
\newtheorem{example}{Example}[section]

\newcommand{\ex}[3]{
\vbox{\vspace{.2cm}
\noindent{\color{black}\rule{\textwidth}{1pt}}
\vspace{-.8cm}
\begin{example}
\hspace*{-1em}\textit{\uline{\hspace*{1em}#1.}}\\
#2
\label{#3}
\end{example}
{\color{black}\rule{\textwidth}{1pt}}\vspace{.2cm}
}
}%

\begin{document}
\lipsum[4]

\ex{This is my example}{\lipsum[5]}{ex:MYEX}
\ex{With a longer title, the thin rule does not stand under the whole text}{\lipsum[7]}{ex:MYEX2}
\ex{In the case I put a long long title, I would like to have the thin rule only on the
piece of title closer to her, in this case only under the underlined text}{\lipsum[3]}{ex:MYEX2}

\end{document}


For what it's worth, I wouldn't use a custom command quite like this for examples. Instead, I would define an example environment and then configure my editor to generate the \begin{example}{...title...}...\end{example} code. I used to use custom commands like you are doing but I found that using more standard latex syntax worked better with my collaborators, it made the journals happier and works better with syntax highlighting in my editor. Of course, there's no harm your using commands like this if you really want to...

Similarly, from a stylistic point of view I think that underlining was used primarily because at the dawn of printing font shapes likes italics, bold, etc were not available. Now that technology has progressed to make these things available, I'd avoid underlining and, instead, put your title into italics and typeset the "text" of the examples as normal text.

• Thanks for your answer, but the \uline command wasn't really what I was looking for, as \uline (a part from the fact that when going to a new line underlines everything, that's not fundamental - and I guess it could be impossible) is too close to the text. Instead I wish to use the rule as it's more like a frame with the thin rule as separator. Plus your advice on the environment is good, but I'm only writing my electromagnetism's notes from lectures, so I have no problem in particular with collaborators :-) (otherwise I wouldn't have make the environment such a messy way). – GiuTeX Jul 18 '17 at 12:10
• Do you know some way to extend the rule on a variable length, rather than underline the text, so that I can decide the space before/after that in order to get the example's style exactly the one I want? Hope I'm not asking too much. – GiuTeX Jul 18 '17 at 12:15