Since @samcarter was faster than me, I had to build some special functionality in my answer, borrowing some code from @egreg (link):
\documentclass[aspectratio=1610]{beamer}
\usepackage[french]{babel}
\usepackage[T1]{fontenc}
\usepackage{amsmath, amsfonts, amssymb}
\usefonttheme[onlymath]{serif}
% from egreg's answer at
%https://tex.stackexchange.com/a/280981/97512
\usepackage{xparse}
\ExplSyntaxOn
\NewDocumentCommand{\xsameword}{m}
{
\leavevmode\maieul_xsameword:n { #1 }
}
\tl_new:N \l__maieul_xsameword_list_tl
\cs_new_protected:Nn \maieul_xsameword:n
{
\tl_set:Nn \l__maieul_xsameword_list_tl
{
\__maieul_xsameword_start:n { #1 }
}
\regex_replace_all:nnN
{ (\c{footnote}\cB..*?\cE.) }
{ \cE\} \1 \c{__maieul_xsameword_start:n} \cB\{ }
\l__maieul_xsameword_list_tl
\tl_use:N \l__maieul_xsameword_list_tl
}
\cs_new_protected:Nn \__maieul_xsameword_start:n
{
\tl_set:Nn \l__maieul_xsameword_list_tl { #1 }
\regex_replace_all:nnN
{ ([^\s,.!?]+) }% Delete what should not be printed again here
{ \c{maieul_xsameword_format:n} \cB\{ \1 \cE\} }
\l__maieul_xsameword_list_tl
\tl_use:N \l__maieul_xsameword_list_tl
}
\NewDocumentCommand{\setxsamewordformat}{m}
{
\cs_set_protected:Nn \maieul_xsameword_format:n { #1 }
}
\ExplSyntaxOff
%%%%%%%%%%%%%%%%%%%%%
\newbox\mytmpbox
\newcommand{\repword}[1]{%
\sbox{\mytmpbox}{#1}%
\hbox to \wd\mytmpbox{\hss\hspace{.2em}''\hss}%
}
\setxsamewordformat{\repword{#1}}
\newcommand{\keepword}[2]{%
\sbox{\mytmpbox}{#2}%
\hbox to \wd\mytmpbox{\hss#1\hss}%
}
\begin{document}
\begin{frame}
Lorsque le \og \textit{carreau carré} \fg{} mesure:
\begin{itemize}
\item {1}\,$cm$ de côté, l'unité d'aire est appelée le \og \textit{\textrm{centimètre carré}} \fg{} et est noté $cm^2$;
\item 1\,\keepword{$dm$}{$cm$} \xsameword{de côté, l'unité d'aire est appelée le} \keepword{\og \textit{\textrm{décimètre carré}} \fg{}}{\og \textit{\textrm{centimètre carré}} \fg{}} \xsameword{et est noté} $cm^2$;
\item 1\,\keepword{$m$}{$cm$} \xsameword{de côté, l'unité d'aire est appelée le} \keepword{\og \textit{\textrm{mètre carré}} \fg{}}{\og \textit{\textrm{centimètre carré}} \fg{}} \xsameword{et est noté} \keepword{$m^2$}{$cm^2$};
\end{itemize}
\end{frame}
\end{document}
This allows you to put a whole sentence in the argument of \xsamewords
and they will be replaced. Also, I built a macro that allows you to insert words taking the same amount of space as another word: \keepwords{this line}{line above}
.

edit: Now I put the first $dm$
in \keepwords
and it is aligned properly, thanks @marmot for pointing me there! :-)
edit2: Example with three lines added.
edit3: Added \usepackage[T1]{fontenc}
and inserted a comment showing which line should be edited to remove, e.g., commas.
The line { ([^\s,.!?]+) }
represents a regular expression
, or short regex
. These can be used for advanced pattern matching, if a simple find
or find & replace
is not sufficient. Here, this regex represents the words that should be replaced by ''
your ditto marks:
[..]
brackets match one character from the inside. [abc]
operated on foobar
would match the b
or the a
(but only one of them, a single character).
[^..]
is the negation of the characters inside, so this regex here is matching anything not present. [^abc]
operated on foobar
would match f
, o
, o
, or r
.
\s
is any white space character (spaces, tabs, sometimes also new lines)
,.!?
are self-explanatory I assume ;)
+
after a character (remember, brackets match a single character) makes the character match as many multiple ("identical") characters, as possible (usual behavior) but at least one. So, e.g., o+
in regex operated on the string fooobar
would match ooo
, not just one or two o
s. [abc]+
operated on foobar
would match ba
.
You can see now, this regex matches everything that is not a white space or punctuation mark, i.e., everything else, that is words with all other special characters. You can think of the symbols inside the negated brackets to be the word delimiters that you still want to print and not replace in this context.
edit4: If you add \leavevmode
before calling (the old) \xsameword
, it should work fine at the beginning of a line as well. Now, I added it in the definition of \xsameword
, so there's no need to take care of it manually.
»
). Also, don't type units in maths italic. Better use thesiunitx
package and the\SI1\cm}
, for instance, to have the correct font and the correct spacing (a thin unbreakable space).$cm^2$
by$\rm cm^2$
. I edited the question to take into account the remarks of Bernard and yourself.\DeclareSIUnit\litre{\ell} \SI{1}{\litre}