Here are two common functions, that can convert the python built-in dictionary into the format of latex/markdown unnumbered list, so that we can copy it into the latex/markdown. Example can be:
Function explanation: it is as simple as it shows. We use the recursion strategy here. When we are going to print a sub-dictionary, we recursively call the function, with more (two) spaces indented.
defdict_to_latex(d, level=0): """ Converts a dictionary to a string in LaTeX format for an unnumbered list. Nested dictionaries are considered as second level items. Args: - d (dict): The dictionary to be converted. - level (int): The current nesting level (defaults to 0). Returns: - A string in LaTeX format for an unnumbered list. """
latex_str = ""
for k, v in d.items(): ifisinstance(k, str): k = k.replace('_', '\_') ifisinstance(v, str): v = v.replace('_', '\_') ifisinstance(v, dict): latex_str += f"{' '*level}\\item {k}\n\\begin{{itemize}}\n{dict_to_latex(v, level+1)}\\end{{itemize}}\n" else: latex_str += f"{' '*level}\\item {k}: {v}\n"
return latex_str
Note that we should avoid the _ in our string. So we need first convert it into escape character, \_.
The result of the string should be wrapped by a \begin{itemize} \end{itemize}.
defdict_to_markdown(d, level=0): """ Converts a dictionary to a string in Markdown format for an unnumbered list. Nested dictionaries are considered as second level items. Args: - d (dict): The dictionary to be converted. - level (int): The current nesting level (defaults to 0). Returns: - A string in Markdown format for an unnumbered list. """
md_str = ""
for k, v in d.items(): ifisinstance(v, dict): md_str += f"{' '*level}- {k}\n{dict_to_markdown(v, level+1)}" else: md_str += f"{' '*level}- {k}: {v}\n"