[Tex/LaTex] Make a variable length horizontal rule depending on the text width of the title

environmentslengthsrulesspacingwidth

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{adjustwidth}{1cm}{1cm}
                \begin{example} 
                    \textit{#1.}\\ 
                    \rule{.5\textwidth}{0.5pt}\\
                        #2 
                    \label{#3}
                \end{example}
            \end{adjustwidth}
        {\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:

enter image description here

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.

Best Answer

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

enter image description here

\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{adjustwidth}{1cm}{1cm}
                \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}
            \end{adjustwidth}
        {\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}