tweh / menukeys

A LaTeX package to typeset menu sequences, key strokes, paths etc.
46 stars 4 forks source link

Make `\hspace`s after `shadowedroundedkeys` and `shadowedangularkeys` unbreakable #67

Closed snipfoo closed 2 years ago

snipfoo commented 2 years ago

Hello,

Both shadowedroundedkeys and shadowedangularkeys key styles insert a \hspace{0.2ex} after the last key.

However, if the key is directly followed by punctuation, this space should be unbreakable so that the punctuation does not wrap to the next line.

See, e.g.:

\documentclass[a4paper]{article}
\usepackage{menukeys}
\renewmenumacro{\keys}{shadowedroundedkeys}
\begin{document}
\noindent Lorem ipsum dolor sit amet, consectetur adipiscing elitus,
sed do eiusmod \keys{Ctrl}, incididunt ut labore et dolore magna aliqua.
\end{document}

which currently produces: shadowedroundedkeys

As proposed in this PR, adding a \nobreak before the \hspace prevents this, and the line doesn't wrap just before the comma.

Thank you!

snipfoo

Skillmon commented 2 years ago

Thanks for your suggestion. However doing this unconditionally is a bad idea, consider the following example in which your proposed solution gives pretty bad results:

\documentclass[border=3.14]{standalone}

\usepackage{menukeys}

% your proposed fix
\makeatletter
\newif\ifaddnobreak\addnobreakfalse
\tw@declare@style@simple*{shadowedroundedkeys}{%
   \tikz[baseline={($(tw@node.base)+(0,-0.2ex)$)}]{%
      \node(tw@node)[tw@shadowedroundedkeys@base]%
         {\strut\color{\usemenucolor{txt}}\CurrentMenuElement};%
   }%
}[%
   \hspace{0.2ex}\hspace{0.1em plus 0.1em minus 0.05em}%
   \textcolor{\usemenucolor{b}}{\raisebox{0.25ex}{\sffamily\relsize{-2}+}}%
   \hspace{0.1em plus 0.1em minus 0.05em}%
][\ifaddnobreak\nobreak\fi\hspace{0.2ex}]{gray}
\makeatother
\renewmenumacro{\keys}{shadowedroundedkeys}

% just to highlight the faulty behaviour
\fboxsep=-\fboxrule

\begin{document}
\fbox{\begin{minipage}{1cm}
  a \keys{Ctrl} abc
\end{minipage}} this is ok
\fbox{\begin{minipage}{1cm}
  \addnobreaktrue
  a \keys{Ctrl} abc
\end{minipage}} this is bad
\end{document}

menutest-1

What can you do instead? Just put an \mbox around the stuff you don't want to break:

\documentclass[preview,border=3.14]{standalone}

\usepackage{menukeys}
\renewmenumacro{\keys}{shadowedroundedkeys}

\begin{document}
\noindent Lorem ipsum dolor sit amet, consectetur adipiscing elitus,
sed do eiusmod \mbox{\keys{Ctrl},} incididunt ut labore et dolore magna aliqua.
\end{document}

menutest-1

snipfoo commented 2 years ago

Hello, and thanks a lot for the prompt feedback!

Whoops, you're totally right! I thought the \nobreak would only apply to the \hspace{0.2ex} and completely forgot it would also make the following spaces non-breaking as well.

(Unfortunately, the \mbox{…} workaround won't do in my precise use case, but that's my problem.)

However, would using a \kern0.2ex instead of the \hspace{0.2ex} be an acceptable solution? Since the length in the \hspace is fixed, it seems to me that we can just use \kern instead, with the extra benefit of making this horizontal space (and only this space) non-breaking.

Adapting from your example, it seems to cover both cases (without and with comma, i.e., cases 2 and 4 below, respectively) correctly:

\documentclass[border=3.14]{standalone}

\usepackage{menukeys}

\makeatletter
\newif\ifusekern\usekernfalse
\tw@declare@style@simple*{shadowedroundedkeys}{%
   \tikz[baseline={($(tw@node.base)+(0,-0.2ex)$)}]{%
      \node(tw@node)[tw@shadowedroundedkeys@base]%
         {\strut\color{\usemenucolor{txt}}\CurrentMenuElement};%
   }%
}[%
   \hspace{0.2ex}\hspace{0.1em plus 0.1em minus 0.05em}%
   \textcolor{\usemenucolor{b}}{\raisebox{0.25ex}{\sffamily\relsize{-2}+}}%
   \hspace{0.1em plus 0.1em minus 0.05em}%
][\ifusekern\kern0.2ex\else\hspace{0.2ex}\fi]{gray}
\makeatother
\renewmenumacro{\keys}{shadowedroundedkeys}

% just to highlight the faulty behaviour
\fboxsep=-\fboxrule

\begin{document}
% without comma
\fbox{\begin{minipage}{1cm} % using \hspace{0.2ex}
  a \keys{Ctrl} abc
\end{minipage}} this is ok
\fbox{\begin{minipage}{1cm} % using \kern0.2ex
  \usekerntrue
  a \keys{Ctrl} abc
\end{minipage}} this is ok

% with comma
\fbox{\begin{minipage}{1cm} % using \hspace{0.2ex}
  a \keys{Ctrl}, abc
\end{minipage}} this is bad
\fbox{\begin{minipage}{1cm} % using \kern0.2ex
  \usekerntrue
  a \keys{Ctrl}, abc
\end{minipage}} this is ok
\end{document}

shadowedroundedkeys-kern

Do you think this would be an acceptable fix? If so, I can adapt my commit accordingly, of course.

Thank you very much!

snipfoo

Skillmon commented 2 years ago

@snipfoo I think changing this to a kerning is a brilliant idea indeed, and shouldn't have any drawbacks except correcting old faulty behaviour. If you adapt your PR I'd be glad to merge and release a new version.

snipfoo commented 2 years ago

@Skillmon Great, thank you so much!

I've just updated the commit in this PR accordingly (94fe6ab).

Thanks!

snipfoo

Skillmon commented 2 years ago

@snipfoo I forgot to ask: Do you want to be named in the documentation and CTAN announcement? If you decide not to, I'd still cite this PR in the CTAN announcement. If you want to (and don't want to post your name here) you might send it per mail to me (if you'd feel more comfortable that way).

snipfoo commented 2 years ago

Hi @Skillmon, thank you for asking!

As far as I'm concerned, I'd rather not be named in the documentation or the CTAN announcement. Just a reference to this PR in the announcement would be perfect for me :)

Thanks a lot!

snipfoo