On category pages,{{Automatic category TOC}} can be placed wherever you want the TOC to be displayed.
On a category header template, it is best placed outside any<includeonly>...</includeonly> tags, so that the template is categorised as using this template.
This template uses themagic word{{PAGESINCATEGORY}} to count the number of pages in the category. Unfortunately, this function can return wildly erroneous results on some categories which have had many pages added and removed.
This bug is well-known, and has been documented in several places since 2010, including:
In the overwhelming majority of categories, the count is recorded accurately enough to support use of this template. However, there seemed to be some issue with{{PAGESINCATEGORY}} falsely returning zero in some cases. This is tracked in: