1

How can I write all url citation in new line using unsrt style. But I don't want use \par in *.bib file (like test1 bibdata).

MWE:

\begin{filecontents}{myRef.bib}
@misc{test,
title = {This is a test},
author = {Mirzakhani, Bahman},
howpublished = {\url{https://tex.stackexchange.com/}},
}
@misc{test1,
title = {This is a test},
author = {Mirzakhani, Bahman},
howpublished = {\par\url{https://tex.stackexchange.com/}},
}
\end{filecontents}

\documentclass[12pt]{article}
\usepackage{hyperref}
\begin{document}

\cite{test}
\cite{test1}

\bibliographystyle{unsrt}
\bibliography{myRef}

\end{document}

Output:

enter image description here

3
  • In my view, the core of the problem is that you use an ancient bibliography style -- unsrt -- which predates the creation of the World Wide Web and hence doesn't know what do with a field called url. You appear to have chosen to use -- mis-use, really, in my view -- the howpublished field for entries of type @misc in order to display suitable URL information. (Presumably, you've come up with further work-arounds for entries of type @article, @book, etc.) Precisely because the howpublished field may contain information other than URL strings, ... (continued in next comment) – Mico Apr 8 at 7:34
  • (continued from preceding comment) ... it is not advisable to edit the code in the unsrt.bst style file to force an automatic line break at the start of the howpublished field. The real solution lies in switching to a more modern bib style -- say, unsrtnat -- that's been programmed to know what do with the contents of url fields and which automatically insert line breaks at the start of the url field. Conversely, if you don't want to switch bib styles, you'll be stuck with inserting \par directives in suitable places. – Mico Apr 8 at 7:38
  • 1
    @Mico, Thank you for your explanation. I will try to use more modern bib style. – Bahman Mirzakhani Apr 8 at 8:20
1

In my view, the core of the problem is that you are using an ancient bibliography style -- unsrt -- which predates the creation of the World Wide Web and hence doesn't know what do with a field called url. You appear to have chosen to use -- mis-use, in my view -- the howpublished field for entries of type @misc in order to display suitable URL information. Presumably, you've come up with further work-arounds for entries of type @article, @book, etc.

Precisely because the howpublished field may contain information other than URL strings, it is not advisable to edit the unsrt.bst style file to force an automatic line break at the start of the howpublished field.

The real solution lies in switching to a more modern bib style -- say, unsrtnat -- that's been programmed to know what do with the contents of url fields, and to edit the bib entries so that URL-related information is contained in the url fields (and nowhere else).

It's straightforward to modify the unsrtnat style to instruct it to insert a line break at the start of url fields. I suggest you proceed as follows:

  • Find the file unsrtnat.bst in your TeX distribution. Make a copy of that file and name the copy, say, unsrtnat-urlbreak.bst. (Don't edit an original file of the TeX distribution directly.

  • Open the file unsrtnat-urlbreak.bst in a text editor -- the program you use to edit your .tex files will do fine -- and locate the function format.url. (In my copy of the file, this function starts on line 285 and occupies all of six lines.

  • In that function, locate the following line:

        { new.block "URL \url{" url * "}" * }
    

    Change it to

        { new.block "\par \url{" url * "}" * }
    

    i.e., replace URL with \par.

  • Save the file in the directory that contains the main tex file.

  • In the main tex file, change

    \bibliographystyle{unsrt}
    

    to

    \bibliographystyle{unsrtnat-urlbreak}
    
  • Run a full recompile cycle -- LaTeX, BibTeX, LaTeX, and LaTeX once more -- to fully propagate all changes.

An MWE and its output:

enter image description here

Observe also that if the year field is non-empty, then information contained in the howpublished field will come before rather than after the year field if the unsrtnat and unsrt bib styles are employed. That's another reason for not using the howpublished field to store URL strings; for sure, it would be bad to generate a line break right before the URL string if the year field information gets separated from the remainder of the formatted bibliographic entry.

\documentclass{article}

\begin{filecontents}[overwrite]{myRef.bib}
@misc{test1,
title   = {This is a test},
author  = {Mirzakhani, Bahman},
year    = 3001,
howpublished = {\url{https://tex.stackexchange.com/}},
}
@misc{test2,
title   = {This is another test},
author  = {Mirzakhani, Bahman},
year    = 3002,
url     = {https://tex.stackexchange.com/},
}
\end{filecontents}

\usepackage[numbers]{natbib}
\bibliographystyle{unsrtnat-urlbreak}
\usepackage{xurl} % for '\url' macro
\usepackage[colorlinks,allcolors=blue]{hyperref}

\begin{document}
\noindent
\cite{test1}, \cite{test2}

\raggedright
\bibliography{myRef}
\end{document}

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.