%Abbreviations
\usepackage[
acronym,%create an acronym-type glossary
toc,%table of contents
nomain,
%savewrites=true,%this option, in combination with the restoreused custom command, messes with backref pagenumbers but they would be needed to suppress single-occurrence abbreviations
%numberline,%snippet for suppressing single-occurrence abbreviations uses this option, dont see its use though.
]{glossaries-prefix}%should be loaded after relevant packages such as hyperref
%loading the glossaries-extra package would have been the better choice in hindsight. It provides professional solutions to many advanced problems I encountered and had to solve with my limited latex skills.


% for special terms that are not acronyms and should be displayed in a special way on first occurence, but not get a glossary entry. I later added the user1=off key and value to those entries - maybe I then would not have needed to sort them into a new type of glossary
\newcommand*{\glossfirstformat}[1]{\textit{#1}}%would prefer to use \emph over \textit here in case first occurrence is in italic text environment, but emph does not work for whatever reason
\newglossary[glignoredl]{ignored}{glignored}{glignoredin}{Ignored Glossary}
%formatting that applies to entries in the [type] glossary only
\defglsentryfmt[ignored]{%
	\ifglsused{\glslabel}{%
		\glsgenentryfmt%
	}{%
		% Typeset first use
		\glossfirstformat{\glsgenentryfmt}%
%		\glsgenentryfmt%
	}%
}
\newglossary[glunignoredl]{unignored}{glunignored}{glunignoredin}{Unignored Glossary}%changed my mind on highlighting some of the terms. Want to keep them in a glossary so I dont have to change occurances in the text
\defglsentryfmt[unignored]{%
	\ifglsused{\glslabel}{%
		\glsgenentryfmt%
	}{%
		% Typeset first use
%		\glossfirstformat{\glsgenentryfmt}%
		\glsgenentryfmt%
	}%
}

%option to change the format of glossary entries
%\renewcommand*{\glstextformat}[1]{\textbf{#1}}%https://tex.stackexchange.com/questions/38544/glossary-links-color
%\renewcommand*{\acronymfont}[1]{\textit{#1}}%https://tex.stackexchange.com/questions/313869/how-to-change-the-font-for-acronym-descriptions-using-glossaries

\makeglossaries%this may need an additional command after the first compile pass. See internet for more
%https://tex.stackexchange.com/questions/101753/specify-perl-location-for-makeglossaries

%suppress glossary style and entry for single occurrence acronyms.
%see https://tex.stackexchange.com/questions/98494/glossaries-dont-print-single-occurences
\glsenableentrycount %then use \cgls commands instead of \gls. Is troublesome with the glossary resets in abstracts and conclusion.
\let\gls\cgls
\let\Gls\cGls
\let\glspl\cglspl
\let\Glspl\cGlspl
%
%Automatic solution to reset glossary-first-use-style (without resetting occurrence counts) per custom chapter might be adaptable from https://tex.stackexchange.com/questions/646615/resetting-glossaries-abbreviations-each-chapter
%but currently requires glossary option savewrites=true, which messes with backref pagenumbers. Falling back to semi-manual \fakefirstacr custom commands
%\newcommand*{\restoreusedflag}{}%command does nothing by default, may get a function depending on conditional
%\preto\glslinkpresetkeys{%would need to adapt this line for use with glossaries-prefix, which does not provide \glslinkpresetkeys. Falling back to a semi-manual solution with a custom \fakefirstacr command which copies the style of a first-use-acronym, with the drawback that I manually need to ensure that the command is used if and only if the full acronym should be displayed. In theory this needs to be done also for acronyms mentioned exactly once after the reset at the end of the abstracts, which is never the case in the current (2025-08-07) version of the document.
%\ifnumcomp{\glsentrycurrcount{\glslabel}}{=}{0}
%{%
%	\ifglsused{\glslabel}
%	{\renewcommand*\restoreusedflag{\csuse{@@glsunset}{\glslabel}}}{}%if the glossary entry was used, possibly before the last glsreset, set the function of \restoreusedflag to marking the entry as used (use the \glsunset command on \glslabel), but do not yet apply \restoreusedflag
%	\glsreset{\glslabel}%
%}{}%
%}
%\preto\glspostlinkhook{%
%	\ifglsused{\glslabel}{}{\restoreusedflag}%if the glossary entry was not used since the last glsreset, apply \restoreusedflag which may or may not have a function
%	\renewcommand*{\restoreusedflag}{}%clear any possible functionalty of the \restoreusedflag command for the next call
%}
%alternative approaches
%https://tex.stackexchange.com/questions/59449/acronym-package-dont-write-acronym-if-single-occurence https://tex.stackexchange.com/questions/651509/how-to-omit-acronyms-with-only-a-single-occurrence-in-acro
%https://tex.stackexchange.com/questions/26238/ignoring-an-acronym-that-is-only-used-once %needs two LaTeX runs to get everything right!

%acronyms
%https://tex.stackexchange.com/questions/232707/modify-appearance-of-first-acronym. see also https://mirrors.mit.edu/CTAN/macros/latex/contrib/glossaries/glossaries-user.pdf
%for linkcolors see https://tex.stackexchange.com/questions/38544/glossary-links-color and https://mirrors.mit.edu/CTAN/macros/latex/contrib/glossaries/glossaries-user.pdf p.226
\newacronymstyle{mystyleit}%custom style which yields italic long form, but roman parentheses and short form
{% use the same display as the long-short style as a basis
	\GlsUseAcrEntryDispStyle{long-short}%
}%
{% use the same definitions as long-short
	\GlsUseAcrStyleDefs{long-short}%
	% Minor modifications:
	\renewcommand*{\genacrfullformat}[1]{%
		\glossfirstformat{\glsentrylong{##1}}\space\textnormal{(\glsentryshort{##1})}%
%		\glsifhyperon{\textcolor{\mylinkcolor}{\textnormal{(\glsentryshort{##1})}}}{\textnormal{(\glsentryshort{##1})}}%thought I broke link colors while setting up the custom style and tried to mess with it even more to get them back. Hopefully it worked without this in the end - still have to check.
	}%
	\renewcommand*{\Genacrfullformat}[2]{%
		\glossfirstformat{\Glsentrylong{##1}}\space\textnormal{(\glsentryshort{##1})}%
%		\glsifhyperon{\textcolor{\mylinkcolor}{\textnormal{(\glsentryshort{##1})}}}{\textnormal{(\glsentryshort{##1})}}%
	}%
	\renewcommand*{\genplacrfullformat}[2]{%
		\glossfirstformat{\glsentrylongpl{##1}}\space\textnormal{(\glsentryshortpl{##1})}%
%		\glsifhyperon{\textcolor{\mylinkcolor}{\textnormal{(\glsentryshortpl{##1})}}}{\textnormal{(\glsentryshortpl{##1})}}%
	}%
	\renewcommand*{\Genplacrfullformat}[2]{%
		\glossfirstformat{\Glsentrylongpl{##1}}\space\textnormal{(\glsentryshortpl{##1})}%
%		\glsifhyperon{\textcolor{\mylinkcolor}{\textnormal{(\glsentryshortpl{##1})}}}{\textnormal{(\glsentryshortpl{##1})}}%
	}%
}
\setacronymstyle{mystyleit}%breaks link colors in the current (2025-07-15) version, including color for references
\defglsentryfmt{%breaks link colors in the current (2025-07-15) version, including color for references
	\ifglshaslong{\glslabel}{%
		\ifglsused{\glslabel}{%
			\glsgenacfmt%\glsifhyperon{\textcolor{\mylinkcolor}{\glsgenacfmt}}{\glsgenacfmt}%
		}{%
			\glossfirstformat{\glsgenacfmt}%
		}%
	}{%
		\ifglsused{\glslabel}{%
			\glsgenentryfmt%
		}{%
			\glossfirstformat{\glsgenentryfmt}%
		}%
	}%
}
\newcommand{\fakefirstacr}[1]{\ifglsused{#1}{\glsdisp{#1}{\genacrfullformat{#1}}}{\gls{#1}}}%manually set an acronym to be displayed as if it was its first use (keep consistent with setacronymstyle), in order to have the long forms displayed e.g. in the conclusion to give clarity to readers which only read the conclusion. Dont want to simply reset uses, such that occurrence counts are kept and thus document-wide single-uses do not have a redundant abbreviation introduced
%uppercase and plural versions:
\newcommand{\fakefirstAcr}[1]{\ifglsused{#1}{\glsdisp{#1}{\Genacrfullformat{#1}}}{\Gls{#1}}}
\newcommand{\fakefirstacrpl}[1]{\ifglsused{#1}{\glsdisp{#1}{\genplacrfullformat{#1}}}{\glspl{#1}}}
\newcommand{\fakefirstAcrpl}[1]{\ifglsused{#1}{\glsdisp{#1}{\Genplacrfullformat{#1}}}{\Glspl{#1}}}
%prefix version:
\newcommand{\fakefirstpacr}[1]{\ifglsused{#1}{\glsentryprefixfirst{#1}\glsdisp{#1}{\genacrfullformat{#1}}}{\pgls{#1}}}
%if needed in the future, define other prefix-glossary commands like pGls equivalently
%
%\glsdisablehyper%disables internal glossary links, can be used for testing
%linking only the short form is nicer, but too much effort. see https://tex.stackexchange.com/questions/127702/how-to-set-hyperfirst-to-just-one-glossary

%may not want to show abbreviations for single uses, see https://tex.stackexchange.com/questions/59449/acronym-package-dont-write-acronym-if-single-occurence in this case. Be mindful, however, of glossary resets such as for e.g. the conclusion
\newacronym{CPMG}{CPMG}{Carr-Purcell-Meiboom-Gill}
\newacronym[
prefixfirst={a\ },%prefix used on first use
prefix={an\ }%prefix used on subsequent use
]{LCST}{LCST}{lower critical solution temperature}%article-included glossary command is \pgls instead of \gls.}
\newacronym{pNIPAM}{pNIPAM}{poly(N-isopropylacrylamide)}
\newacronym[description={Poly(N-isopropylacrylamid); German for pNIPAM}]{pNIPAMgerman}{\mbox{PNIPAM}}{\foreignlanguage{ngerman}{Poly(N-isopropylacrylamid)}}%pNIPAM in German
\newacronym{TSUA}{TSUA}{test single-use acronym}
\newacronym{TMUSC}{TMUSC}{test multiple-use single in conclusion}
\newacronym{TSUC}{TMUSC}{test single-use in conclusion}

%special terms that are not acronyms. use the user1 key to mark entry for non-hyperlink
%syntax example: \newglossaryentry{variable name}{name=word), type=ignored, description={}, user1={off}}
\newglossaryentry{athermal}{name=athermal, type=ignored, description={}, user1={off}}
\newglossaryentry{complexation}{name=complexation, type=ignored, description={}, user1={off}}% as in solvent complexation
\newglossaryentry{Rh}{name=hydrodynamic radius, first=hydrodynamic radius \textnormal{(\Rh)}, prefixfirst={a\ }, prefix={an\ }, plural=hydrodynamic radii, type=unignored, description={}, user1={off}}%first set to belong to the ``ignored'' glossary for special formatting on first occurrence. Then I later changed my mind and decided that the reader is probably already familiar with the term ``hydrodynamic radius'' and therefore changed the type of glossary the entry should be assigned to to the ``unignored'' glossary
\newglossaryentry{Larmor frequency}{name={Larmor frequency}, first={Larmor frequency \textnormal{(\wL)}}, plural={Larmor frequencies}, firstplural={Larmor frequencies \textnormal{(\wL)}}, type={ignored}, description={}, user1={off}}
\newglossaryentry{spectral density}{name=spectral density, plural=spectral densities, type=ignored, description={}, user1={off}}
\newglossaryentry{Tdem}{name={demixing temperature}, type=ignored, description={}, user1={off}}

%disable links for for glossary entries that have a value 'off' to the key 'user1'
\renewcommand*{\glslinkcheckfirsthyperhook}{%
	% Get the value of the user1 field
	\glsletentryfield{\thisvalue}{\glslabel}{useri}%
	% Test this value
	\ifdefstring\thisvalue{off}%
	{\setkeys{glslink}{hyper=false}}%
	{}%
}