1

I am using roman numbers for the sections and latin letters for the subsections (without the latin letter for the section). Naturally, the subsection A appears multiple times now. I reference to those \ref{sec:title}. The output is always A, regardless to which subsection I reference. I wish to reference to a specific subsection with the roman number preceding the letter. Attached, you find a minimal working example. I already tried to adjust this by adding \def\p@subsection{}.

Any help would be appreciated!

Best

Daniel

\documentclass[12pt]{article}
\usepackage{alphalph}
\usepackage[toc,page]{appendix}
\usepackage{array}
\usepackage{amsmath,amsthm,verbatim,amssymb,amsfonts,amscd, graphicx}
\usepackage[toc,page]{appendix}
\usepackage{booktabs}
\usepackage{cite}
\usepackage{graphics}
\usepackage{etoolbox}
\usepackage{lscape}
\usepackage{multirow}
\usepackage[authoryear,round,url=false]{natbib}
%\usepackage[authordate]{biblatex-chicago}

\usepackage{rotating}
\usepackage[doublespacing]{setspace}
\usepackage[colorlinks,citecolor=blue, linkcolor=blue]{hyperref}
\usepackage{subcaption}
%\usepackage{subfig}
\usepackage{threeparttable}
\usepackage[tableposition=top]{caption}
\usepackage[section]{placeins}
\usepackage[multiple]{footmisc}
\usepackage[left=1in, right=1in, top=1in, bottom=1in]{geometry}
\usepackage[T1]{fontenc}
\usepackage[utf8]{inputenc}

\setlength{\parindent}{5ex}


% *****************************************************************
% Custom subcaptions
% *****************************************************************
% Note/Source/Text after Tables
\newcommand{\figtext}[1]{
    \vspace{0.5ex}
    \captionsetup{textfont={small,singlespacing}}
    \caption*{\hspace{0.5ex}\hangindent=1.5em #1}
    }
\newcommand{\fignote}[1]{\figtext{\emph{Note:~}~#1}}

\newcommand{\figsource}[1]{\figtext{\emph{Source:~}~#1}}

% Add significance note with \starnote
\newcommand{\starnote}{\figtext{* p $<$ 0.1, ** p $<$ 0.05, *** p $<$ 0.01. Standard errors in parentheses.}}
%\renewcommand{\footnotesize}{\fontsize{12pt}{24}\selectfont}
%\renewcommand*{\footnotelayout}{\normalsize\doublespacing}
% *****************************************************************
% Cnew column types
% *****************************************************************
\newcolumntype{L}[1]{>{\raggedright\let\newline\\\arraybackslash\hspace{0pt}}m{#1}}
\newcolumntype{C}[1]{>{\centering\let\newline\\\arraybackslash\hspace{0pt}}m{#1}}
\newcolumntype{R}[1]{>{\raggedleft\let\newline\\\arraybackslash\hspace{0pt}}m{#1}}

\renewcommand*{\thesubfigure}{%
\alphalph{\value{subfigure}}%
}%


\makeatletter

% Patch case where name and year are separated by aysep
\patchcmd{\NAT@citex}
  {\@citea\NAT@hyper@{%
     \NAT@nmfmt{\NAT@nm}%
     \hyper@natlinkbreak{\NAT@aysep\NAT@spacechar}{\@citeb\@extra@b@citeb}%
     \NAT@date}}
  {\@citea\NAT@nmfmt{\NAT@nm}%
   \NAT@aysep\NAT@spacechar\NAT@hyper@{\NAT@date}}{}{}

% Patch case where name and year are separated by opening bracket
\patchcmd{\NAT@citex}
  {\@citea\NAT@hyper@{%
     \NAT@nmfmt{\NAT@nm}%
     \hyper@natlinkbreak{\NAT@spacechar\NAT@@open\if*#1*\else#1\NAT@spacechar\fi}%
       {\@citeb\@extra@b@citeb}%
     \NAT@date}}
  {\@citea\NAT@nmfmt{\NAT@nm}%
   \NAT@spacechar\NAT@@open\if*#1*\else#1\NAT@spacechar\fi\NAT@hyper@{\NAT@date}}
  {}{}
    
\renewcommand\thesection{\Roman{section}}
\renewcommand\thesubsection{\Alph{subsection}}
\renewcommand\@seccntformat[1]{\csname the#1\endcsname. }

\def\p@subsection{}

\makeatother

\begin{document} 

\section{Hello}\label{sec:hello}
\subsection{World}\label{sec:world}

I test the reference to subsection \ref{sec:areyou}
\section{How}\label{sec:how}
\subsection{are you?}\label{sec:areyou}
\end{document}
2
  • If you only want to affect \label and \ref, you need to set \@currentlabel to the desired value after \subsection and before \label. Mar 25, 2021 at 13:20
  • You might also look at the cleverref package. Of course, you will have to use \cref instead of \ref. Mar 25, 2021 at 13:57

1 Answer 1

0

Because \subsection simply calls \@startsection with the arguments passed on, etoolbox isn't much help here.

\documentclass[12pt]{article}
\usepackage{alphalph}
\usepackage[toc,page]{appendix}
\usepackage{array}
\usepackage{amsmath,amsthm,verbatim,amssymb,amsfonts,amscd, graphicx}
\usepackage[toc,page]{appendix}
\usepackage{booktabs}
\usepackage{cite}
\usepackage{graphics}
\usepackage{etoolbox}
\usepackage{lscape}
\usepackage{multirow}
\usepackage[authoryear,round]{natbib}% error with url=false
%\usepackage[authordate]{biblatex-chicago}

\usepackage{rotating}
\usepackage[doublespacing]{setspace}
\usepackage[colorlinks,citecolor=blue, linkcolor=blue]{hyperref}
\usepackage{subcaption}
%\usepackage{subfig}
\usepackage{threeparttable}
\usepackage[tableposition=top]{caption}
\usepackage[section]{placeins}
\usepackage[multiple]{footmisc}
\usepackage[left=1in, right=1in, top=1in, bottom=1in]{geometry}
\usepackage[T1]{fontenc}
\usepackage[utf8]{inputenc}

\setlength{\parindent}{5ex}


% *****************************************************************
% Custom subcaptions
% *****************************************************************
% Note/Source/Text after Tables
\newcommand{\figtext}[1]{
    \vspace{0.5ex}
    \captionsetup{textfont={small,singlespacing}}
    \caption*{\hspace{0.5ex}\hangindent=1.5em #1}
    }
\newcommand{\fignote}[1]{\figtext{\emph{Note:~}~#1}}

\newcommand{\figsource}[1]{\figtext{\emph{Source:~}~#1}}

% Add significance note with \starnote
\newcommand{\starnote}{\figtext{* p $<$ 0.1, ** p $<$ 0.05, *** p $<$ 0.01. Standard errors in parentheses.}}
%\renewcommand{\footnotesize}{\fontsize{12pt}{24}\selectfont}
%\renewcommand*{\footnotelayout}{\normalsize\doublespacing}
% *****************************************************************
% Cnew column types
% *****************************************************************
\newcolumntype{L}[1]{>{\raggedright\let\newline\\\arraybackslash\hspace{0pt}}m{#1}}
\newcolumntype{C}[1]{>{\centering\let\newline\\\arraybackslash\hspace{0pt}}m{#1}}
\newcolumntype{R}[1]{>{\raggedleft\let\newline\\\arraybackslash\hspace{0pt}}m{#1}}

\renewcommand*{\thesubfigure}{%
\alphalph{\value{subfigure}}%
}%


\makeatletter

% Patch case where name and year are separated by aysep
\patchcmd{\NAT@citex}
  {\@citea\NAT@hyper@{%
     \NAT@nmfmt{\NAT@nm}%
     \hyper@natlinkbreak{\NAT@aysep\NAT@spacechar}{\@citeb\@extra@b@citeb}%
     \NAT@date}}
  {\@citea\NAT@nmfmt{\NAT@nm}%
   \NAT@aysep\NAT@spacechar\NAT@hyper@{\NAT@date}}{}{}

% Patch case where name and year are separated by opening bracket
\patchcmd{\NAT@citex}
  {\@citea\NAT@hyper@{%
     \NAT@nmfmt{\NAT@nm}%
     \hyper@natlinkbreak{\NAT@spacechar\NAT@@open\if*#1*\else#1\NAT@spacechar\fi}%
       {\@citeb\@extra@b@citeb}%
     \NAT@date}}
  {\@citea\NAT@nmfmt{\NAT@nm}%
   \NAT@spacechar\NAT@@open\if*#1*\else#1\NAT@spacechar\fi\NAT@hyper@{\NAT@date}}
  {}{}
    
\renewcommand\thesection{\Roman{section}}
\renewcommand\thesubsection{\Alph{subsection}}
\renewcommand\@seccntformat[1]{\csname the#1\endcsname. }

\def\p@subsection{}

\let\oldsubsection=\subsection
\renewcommand{\subsection}[2][\empty]{%
\ifx\empty#1\relax
  \oldsubsection[#2]{#2}%
\else
  \oldsubsection[#1]{#2}%
\fi
\edef\@currentlabel{\thesection\thesubsection}}

\makeatother

\begin{document} 

\section{Hello}\label{sec:hello}
\subsection{World}\label{sec:world}

I test the reference to subsection \ref{sec:areyou}
\section{How}\label{sec:how}
\subsection{are you?}\label{sec:areyou}
\end{document}
1
  • Thanks John, this works like a charm.
    – Daniel2805
    Mar 25, 2021 at 14:32

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .