1

With or without the use of titlesec package, how it's possible to put the section number (i.e. \thesection) on the right margin? I have literally no idea. I have tried to do some experiments but without any luck.

MWE:

%!TEX program = xelatex
\documentclass[
    draft=false,     % final mode 
    paper=portrait,
    twoside=false,
    open=right,
    parskip=full,
    headings=big,
    version=last, %
    italian, % language (passed to babel and other packages)
            % (ngerman, english, french, ...)
]{scrbook} % Classes: scrartcl, scrreprt, scrbook

% ----------------------
% DIMENSIONI PAGINA
% ----------------------
\setlength{\oddsidemargin}{0.3cm}
\setlength{\textwidth}{0.7\textwidth}
\setlength{\marginparwidth}{2\marginparwidth}


\usepackage{lmodern}
\usepackage{amsmath}  % advanced math symbols pkg
\usepackage{amsthm, amsthm}
\usepackage[utf8]{inputenc} % utf8 encoding
\usepackage{graphicx} % pictures support
\usepackage{longtable} % table on multiple pages support
\usepackage[italian]{babel} % language support
\usepackage{enumitem} % custom enumeration support
\usepackage{rotating} % Rotating pictures support
\usepackage{floatrow} % Support for floated images
\usepackage{hyperref} % Support for href in toc
\usepackage{marvosym} % Currency symbols
\usepackage{fancyhdr} % Fancy headers
\usepackage{array}
\usepackage{footnote}
\usepackage{setspace}
\usepackage{afterpage} % Permette di mettere i numeri di pagina nel footer se si usa il package Fancyhdr
\usepackage{textcomp} % Package for copyright glyph
\usepackage[newfloat]{minted} % Package for highlight code
\usepackage{appendix} %Package for appendices
\usepackage{chngcntr} %Package for list of figures numeration
\usepackage{csquotes}
\usepackage{fontspec}
\usepackage[protrusion=true]{microtype}
\usepackage{mwe}


% ----------------------
% CUSTOMIZATION CAPITOLI
% ----------------------
\usepackage{xcolor}
\usepackage{graphicx}
\usepackage{etoolbox}
\usepackage{blindtext}

%\input{preamble/style-chapter}

\renewcommand*{\sectionformat}{%
  \llap{\thesection\enskip}%
}

\renewcommand*{\subsectionformat}{%
  \llap{\thesubsection\enskip}%
}

%\renewcommand*{\raggedsection}{\raggedleft} % Allinea a destra i titoli
\usepackage[explicit]{titlesec}
\titleformat{\section}
  {\raggedleft\normalfont\Large\bfseries} % format
  %{\makebox[0pt][l]{ % label
  % \makebox[\dimexpr\textwidth+\marginparsep-1.3cm][r]{
  %   \thesection
  % }}
  %}
  {\makebox[0pt][r]{#1 \hspace*{0.01cm}\thesection}}
  {0pt} % sep
  {\phantomsection} % before

% ----------------------
% FOOTNOTE AL MARGINE
% ----------------------
\usepackage{marginfix} % --> gestisce correttamente il posizionamento ai margini
\usepackage[%
   bottom,      % Footnotes appear always on bottom. This is necessary
   stable,      % Make footnotes stable in section titles
   perpage,     % Reset on each page
   side,       % Place footnotes in the margin
   ragged,      % Use RaggedRight
   marginal,
   norule,     % suppress rule above footnotes
   multiple,    % rearrange multiple footnotes intelligent in the text.
]{footmisc}
\setlength{\footnotemargin}{0.1mm} % --> distanza numero di nota a pie di pagine e testo
\definecolor{footnotecolor}{RGB}{78, 78, 78}
\def\footnotelayout{\raggedright\color{footnotecolor}} % --> raggedright coincide con text-align:left


% ----------------------
% CAPTION CUSTOMIZATION
% ----------------------
\usepackage[
    format=plain, 
    justification=raggedright,
    singlelinecheck=false
]{caption}
\captionsetup{figurename=FIGURE, labelsep=colon}
\captionsetup[figure]{font={color=footnotecolor,it,footnotesize},labelfont={color=black,bf,it,scriptsize}}
\captionsetup[listing]{font={color=footnotecolor,it,footnotesize},labelfont={color=black,bf,it,scriptsize}}
\renewcommand*{\figureformat}{ % --> elimina un punto di troppo sulla numerazione delle figure
    \figurename~\thefigure
       %  \autodot% DELETED
}

% ----------------------
% FLOATING
% ----------------------
\floatsetup{
    margins=hangright,
    capposition=beside,
    capbesideposition={bottom,right},
    floatwidth=\textwidth
}

\begin{document}
  \setminted{linenos,autogobble, frame=lines, framesep=3mm, tabsize=2, fontsize=\footnotesize,baselinestretch=1} %IMPOSTAZIONI 
  \chapter{Chapter}
  \blindtext\footnote{Lorem ipsum dolor sit amet, consectetur adipiscing elit. Cras accumsan rutrum dictum. Nunc volutpat egestas 
  ante non efficitur. Sed pellentesque vulputate scelerisque.}
  \blindtext
  \section{Section}
  \blindtext
\end{document}

This is the current result: Current result

This is what I would like to achieve: Desidered result

8
  • 2
    Using titlesec with scrbook is discouraged (and might even be disallowed in future releases).
    – egreg
    Nov 18 '17 at 17:28
  • @egreg yes, I read more than once, but I don't know any alternatives. Achieving my same result with the KOMA-Script features I think would be 20x more lines of code!
    – toioski
    Nov 18 '17 at 17:32
  • @toioski I would rather say about 3x, but if you are that restrictive about the number of lines...
    – TeXnician
    Nov 18 '17 at 17:36
  • @TeXnician I am not restrictive, never said it. Mine was just a consideration. As you can read from the question, I don't really care about the type of solution.
    – toioski
    Nov 18 '17 at 17:38
  • Do you want it on the right side on each page or just on odd pages?
    – TeXnician
    Nov 18 '17 at 17:39
4

You can redefine \sectionlinesformat:

\renewcommand*\sectionformat{\textcolor{blue}{\thesection\autodot}}
\addtokomafont{section}{\rmfamily}
\makeatletter
\renewcommand*\sectionlinesformat[4]{%
  \ifstr{#1}{section}
    {\raggedleft#4\makebox[0pt][l]{\hskip\marginparsep#3}}
    {\@hangfrom{\hskip#2#3}#4}% use original definition for subsection, subsubsection etc.
}
\makeatother

Example without the unrelated packages and settings:

\documentclass[
    draft=false,
    paper=portrait,
    twoside=false,
    open=right,
    parskip=full,
    headings=big,
    version=last,
    %italian,
]{scrbook} % Classes: scrartcl, scrreprt, scrbook


\usepackage{lmodern}
\usepackage{xcolor}
\usepackage[protrusion=true]{microtype}
\usepackage{blindtext}% only for dummy text
\usepackage{showframe}% to show the page layout
\usepackage{hyperref} % load as last package

\renewcommand*\sectionformat{\textcolor{blue}{\thesection\autodot}}
\addtokomafont{section}{\rmfamily}
\makeatletter
\renewcommand*\sectionlinesformat[4]{%
  \ifstr{#1}{section}
    {\raggedleft#4\makebox[0pt][l]{\hskip\marginparsep#3}}
    {\@hangfrom{\hskip#2#3}#4}% use original definition for subsection, subsubsection etc.
}
\makeatother

\begin{document}
\chapter{Chapter}
\Blindtext[2]
\section{Section}
\blindtext
\end{document}

Result:

enter image description here

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.