How to set rightmark after \section command?

I found an approach here (vie the linked page). Seems to work but I wonder if there is a way without changing my code?

\documentclass{book}

\usepackage{lipsum}

\begin{document}
    \tableofcontents
    \chapter{Test}
    \lipsum
    \section{A Section}
    \lipsum[3-5]
    \section[% necessary to keep \sectionmark away from ToC
        A very long section title, which doesn't fit in
        the header because it's extremly long
    ]{%
        A very long section title, which doesn't fit in
        the header because it's extremly long%
        \sectionmark{Short title}% changes the head for the current page
    }
    \sectionmark{Short title}% changes the head for following pages
    \lipsum[1-14]
\end{document}

A possibility is to have a syntax such as

\section[toc entry]{title}[header entry]

that is similar to yours. Here's a way:

\documentclass{book}

\usepackage{xparse,lipsum}

\makeatletter
\let\tobi@kept@section\section
\RenewDocumentCommand{\section}{s}
  {\IfBooleanTF{#1}{\tobi@kept@ssection*}{\tobi@section}}
\NewDocumentCommand{\tobi@section}{o m o}
  {\IfNoValueTF{#3}% No short title for the header
     {\IfNoValueTF{#1}% No title for the toc
        {\tobi@kept@section{#2}}
        {\tobi@kept@section[#1]{#2}}%
     }
     {\IfNoValueTF{#1}% No title for the toc
        {\tobi@kept@section[#2]{#2\sectionmark{#3}}\sectionmark{#3}}
        {\tobi@kept@section[#1]{#2\sectionmark{#3}}\sectionmark{#3}}%
    }%
  }
\makeatother

\begin{document}
\tableofcontents
\chapter{Test}
\lipsum

\section{A Section}

\lipsum[3-5]

\section
  [A very long section title, which doesn't fit
   in the header because it's extremely long]
  {A very long section title, which doesn't fit\\
   in the header because it's extremly long}[Short title]

\lipsum[1-12]

\section
  {A very long section title, which doesn't fit
   in the header because it's extremly long}[Short title, but longer]

\lipsum[1-14]

\end{document}

The approach of memoir is

\section[toc entry][header entry]{title}

but I prefer to have later the header entry (it's also easier to realize it with xparse).


If using titlesec is possible, just write:

\usepackage[toctitles]{titlesec}

With that, the optional argument in sectioning commands only is used for the header, while for the TOC entry the full title is used.