![如何转义命令定义中使用的变量内的字符?](https://linux22.com/image/458047/%E5%A6%82%E4%BD%95%E8%BD%AC%E4%B9%89%E5%91%BD%E4%BB%A4%E5%AE%9A%E4%B9%89%E4%B8%AD%E4%BD%BF%E7%94%A8%E7%9A%84%E5%8F%98%E9%87%8F%E5%86%85%E7%9A%84%E5%AD%97%E7%AC%A6%EF%BC%9F.png)
我希望我的参考文献的 doi 部分(APA 格式)看起来像这样:
doi: <hyperlinked doi>
\renewcommand{\doi}[1]{\href{https://doi.org/#1}{#1}}
为了实现这一点,我在 后面添加了命令\usepackage{doi}
。99% 的情况下,这个命令都能正常工作,但是当涉及到带有下划线的 dois 时,我收到编译错误:
Missing $ inserted. ...PACrefDOI} \doi{problem_doi}
。
我发现解决这个问题的唯一方法是手动转义 .bbl 文件中的下划线。然而,我希望有一种方法可以调整我的 doi 命令,以便它自动为我转义下划线。这是我的 MWE:
\documentclass[12pt,a4paper]{article}
\usepackage[natbibapa]{apacite}
\bibliographystyle{apacite}
\usepackage[colorlinks,citecolor=blue,urlcolor=blue, linkcolor=blue, bookmarks=false,hypertexnames=true]{hyperref}
\usepackage{doi}
\renewcommand{\doi}[1]{\href{https://doi.org/#1}{#1}}
\begin{document}
\cite{notaraParentalWeightStatus2019}
\bibliography{mwe}
\end{document}
其中 mwe.bib 包含:
@article{notaraParentalWeightStatus2019,
title = {Parental Weight Status and Early Adolescence Body Weight in Association with Socioeconomic Factors},
author = {Notara, Venetia and Magriplis, Emmanuella and Prapas, Christos and Antonogeorgos, George and {Rojas-Gil}, Andrea Paola and Kornilaki, Ekaterina N. and Lagiou, Areti and Panagiotakos, Demosthenes B.},
year = {2019},
journal = {Journal of Education and Health Promotion},
volume = {8},
pages = {77},
issn = {2277-9531},
doi = {10.4103/jehp.jehp_14_19},
abstract = {BACKGROUND: Childhood obesity remains a major health issue. The understanding of the multifactorial nature of childhood obesity remains the cornerstone to eliminate the rising trends. This study aimed to examine the association between parental and childhood weight status, in relation to various socioeconomic (SE) factors. METHODS: A cross-sectional survey was conducted including 1190 children aged 10-12 years and their parents, during school years 2014-2016. Primary schools from five Greek counties (including Athens metropolitan area) were randomly selected. Parental and child data were collected through self-administered, anonymous questionnaires. Children's weight status was based on gender- and age-specific tables derived from the International Obesity Task Force body mass index (BMI) cut offs. General Linear Model (GLM), Univariate and multivariate analyses were applied. Multiple logistic regressions was used to determine the association between children and parents' weight status. RESULTS: Childhood prevalence of overweight and obesity was 25.9\% (21.8\% overweight and 4.1\% obese), with prevalence being significantly higher in males (31.7\% compared to 21.3\%; P for gender differences {$<$} 0.001). The percent of overweight and obese male (34.4\% and 43.1\%) and female children (20.3\% and 31.8\%) significantly increased with paternal overweight and obesity status, respectively. The same relationship was observed between male children and maternal overweight and obesity status (43.4\% and 65.7\%). This was not evident among females (27\% and 23.2\%). Regression analysis showed a significant positive association with parental BMI, a negative association with both parental educational levels (low to high), living space, and parental age (P {\$<\$} 0.05, for all). Children's likelihood of being overweight or obese increased significantly with increasing parental weight status (P {\$<\$} 0.001). CONCLUSIONS: Parental weight status remained the most significant predictive factor for early adolescence obesity among various SE factors. Health promotion strategies should consider parental education as an effective childhood obesity preventive measure.},
langid = {english},
pmcid = {PMC6512222},
pmid = {31143794},
keywords = {Children weight status,health promotion,obesity risk factors,parental weight status,socioeconomic factors}
}
答案1
您需要\detokenize
在定义的第二部分中使用参数\doi
来删除字符的 TeX 特殊含义,例如_
(“下划线”)。
\renewcommand{\doi}[1]{\href{https://doi.org/#1}{\detokenize{#1}}}
并且,如果您使用 pdfLaTeX 来编译您的文档,请使用该fontenc
选项加载包T1
以确保_
正确打印去标记化的字符。
\documentclass[12pt,a4paper]{article}
\begin{filecontents}[overwrite]{mwe.bib}
@article{notaraParentalWeightStatus2019,
title = {Parental Weight Status and Early Adolescence Body Weight in Association with Socioeconomic Factors},
author = {Notara, Venetia and Magriplis, Emmanuella and Prapas, Christos and Antonogeorgos, George and Rojas-Gil, Andrea Paola and Kornilaki, Ekaterina N. and Lagiou, Areti and Panagiotakos, Demosthenes B.},
year = {2019},
journal = {Journal of Education and Health Promotion},
volume = {8},
pages = {77},
issn = {2277-9531},
doi = {10.4103/jehp.jehp_14_19},
langid = {english},
pmcid = {PMC6512222},
pmid = {31143794},
}
\end{filecontents}
\usepackage[T1]{fontenc} % <-- new
\usepackage[natbibapa]{apacite}
\bibliographystyle{apacite}
\usepackage[colorlinks,allcolors=blue,
bookmarks=false,hypertexnames=true]%
{hyperref}
%\usepackage{doi} % <-- do you really need this package?
\providecommand{\doi}[1]{\href{https://doi.org/#1}{\detokenize{#1}}}
\hyphenation{socio-econo-mic}
\begin{document}
\citet{notaraParentalWeightStatus2019}
\bibliography{mwe}
\end{document}
答案2
首先加载 doi 包(处理这种情况会遇到很多麻烦),然后再次覆盖其命令,这看起来有点没意义。如果您只想删除包添加的文本,请重新定义\doitext
:
\documentclass[12pt,a4paper]{article}
\usepackage[natbibapa]{apacite}
\bibliographystyle{apacite}
\usepackage[colorlinks,citecolor=blue,urlcolor=blue, linkcolor=blue, bookmarks=false,hypertexnames=true]{hyperref}
\usepackage{doi}
\renewcommand{\doitext}{}
\begin{document}
\cite{notaraParentalWeightStatus2019}
\bibliography{mwe}
\end{document}