The error ! LaTeX Error: Font family T1+lmr' unknown. arises because of the interaction between fontspec and the legacy font loading mechanism that lmodern and your \DeclareFontShape commands rely on.
When you load fontspec, it takes primary control over font selection, typically setting up fonts for Unicode encoding (TU). The config option tells fontspec to load its machinery but not to immediately set up a default document font.
Then, \RequirePackage{lmodern} tries to set up Latin Modern fonts using the traditional T1 encoding.
Subsequently, \normalfont is called. At this point, with lualatex and fontspec loaded, the system might be in a state where it's not correctly recognizing the T1 encoded lmr family that lmodern should have defined, especially before fontspec has fully configured a main font.
The simplest way to resolve this specific error, while keeping your font declarations, is to ensure that the lmodern package (which defines T1/lmr) is loaded before fontspec. This way, the legacy font setup is done first.
Here's the corrected order:
\documentclass{article}
% Load lmodern first to establish T1/lmr before fontspec takes over
\RequirePackage{lmodern}
% Then load fontspec
\RequirePackage[config,no-math]{fontspec}
% \normalfont here will select the defaults set by lmodern (T1/lmr)
% because fontspec was loaded with 'config' and hasn't set its own main font yet.
\normalfont
% These declarations should now find T1/lmr defined by the lmodern package
\DeclareFontShape{T1}{lmr}{m}{scit}{<->ssub*lmr/m/scsl}{}
\DeclareFontShape{T1}{lmr}{bx}{sc}{<->ssub*cmr/bx/sc}{} % Note: cmr here, was this intended? Or lmr?
\DeclareFontShape{T1}{lmr}{bx}{scit}{<->ssub*cmr/bx/scit}{} % cmr here
\DeclareFontShape{T1}{lmr}{bx}{scsl}{<->ssub*cmr/bx/scsl}{} % cmr here
\begin{document}
asbjkegfliegu
\textit{\textsc{Test Small Caps Italic}} % This would use lmr/m/scit -> lmr/m/scsl
{\bfseries\textsc{Test Bold Small Caps}} % This would use lmr/bx/sc -> cmr/bx/sc
\end{document}
Explanation of the fix:
\RequirePackage{lmodern}: This loads the Latin Modern fonts using the traditional NFSS (New Font Selection Scheme) and makes T1/lmr (Latin Modern Roman with T1 encoding) available. It also typically sets this as the default.
\RequirePackage[config,no-math]{fontspec}: fontspec is loaded. The config option is important because it prevents fontspec from immediately overriding the default font family with a Unicode font. no-math prevents fontspec from touching math font setup.
\normalfont: This command re-selects the current default font settings. Since lmodern was loaded first and fontspec hasn't yet set a new default main font (due to config), \normalfont should stick with T1/lmr.
\DeclareFontShape{...}: These commands now modify the T1/lmr family, which has been properly defined by lmodern and is recognized.
Important Considerations:
- Mixing
fontspec and legacy encodings: While this solution makes your specific code compile, be aware that actively mixing fontspec (which prefers Unicode OpenType/TrueType fonts) with explicit T1 encoding and legacy font package commands can sometimes lead to complex interactions.
- Purpose of
fontspec: If your ultimate goal is to use OpenType Latin Modern fonts via fontspec, you would typically do:
\documentclass{article}
\RequirePackage{fontspec} % no 'config' needed if setting main font right away
\setmainfont{Latin Modern Roman} % Or specific LM font name
% \usepackage{lmodern} % Would not be needed
% \DeclareFontShape for T1 would not apply to the main text font (which is now TU encoded)
% You would use fontspec's features to define shape variants if needed.
% For example, to fake small caps italic for Latin Modern Roman:
% \setmainfont{Latin Modern Roman}[
% SmallCapsFeatures = {Letters=SmallCaps},
% ItalicFeatures = {Letters=SmallCaps} % This applies small caps to the italic font
% ]
% Then \textit{\textsc{text}} or a custom command might work.
\begin{document}
asbjkegfliegu
\end{document}
However, your \DeclareFontShape lines specifically target T1/lmr, suggesting you intend for those specific legacy definitions to be active.
- Substitutions to
cmr: Your bx shapes for lmr are substituting to cmr (Computer Modern Roman) bold shapes:
\DeclareFontShape{T1}{lmr}{bx}{sc}{<->ssub*cmr/bx/sc}{}.
This means if you request bold small caps for Latin Modern, you'll get bold small caps from Computer Modern. This is a valid substitution if lmr/bx/sc isn't available, but ensure it's your intention. Latin Modern does have bold small caps (e.g., ec-lmbxsc10.tfm maps to lmbxsc10.pfb). The issue might be if the .fd file t1lmr.fd doesn't directly define bx/sc but cmr's .fd file does. Often, lmodern tries to provide a fuller set, so substituting to lmr/bx/scsl or similar might also be an option if direct lmr/bx/sc is elusive.
The provided reordering should make your original MWE compile as intended with lualatex.